janito 3.3.0__py3-none-any.whl → 3.4.0__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 (214) hide show
  1. janito/cli/cli_commands/list_plugins.py +48 -52
  2. janito/cli/core/getters.py +0 -3
  3. janito/cli/main_cli.py +12 -9
  4. janito/drivers/openai/driver.py +0 -1
  5. janito/drivers/zai/driver.py +0 -1
  6. janito/hello.txt +0 -0
  7. janito/llm/auth_utils.py +5 -14
  8. janito/plugins/__init__.py +7 -18
  9. janito/{plugins_backup_20250825_070018 → plugins}/auto_loader_fixed.py +11 -12
  10. janito/plugins/base.py +152 -57
  11. janito/plugins/builtin.py +1 -15
  12. janito/{plugins_backup_20250825_070018 → plugins}/core_adapter.py +9 -11
  13. janito/plugins/core_loader.py +34 -58
  14. janito/{plugin_system_backup_20250825_070018/core_loader.py → plugins/core_loader_fixed.py} +26 -45
  15. janito/plugins/discovery.py +3 -3
  16. janito/tools/__init__.py +7 -31
  17. janito/tools/adapters/__init__.py +1 -6
  18. janito/tools/adapters/local/__init__.py +70 -7
  19. janito/{plugins_backup_20250825_070018/tools → tools/adapters/local}/ask_user.py +3 -3
  20. janito/{plugins_backup_20250825_070018/tools → tools/adapters/local}/create_file.py +4 -4
  21. janito/{plugins/core/filemanager/tools → tools/adapters/local}/delete_text_in_file.py +0 -1
  22. janito/{plugins_backup_20250825_070018/tools → tools/adapters/local}/fetch_url.py +3 -3
  23. janito/{plugins_backup_20250825_070018/tools → tools/adapters/local}/replace_text_in_file.py +4 -4
  24. janito/{plugins_backup_20250825_070018/tools → tools/adapters/local}/show_image.py +6 -15
  25. janito/{plugins/core/imagedisplay/tools → tools/adapters/local}/show_image_grid.py +5 -13
  26. janito/tools/base.py +1 -31
  27. janito/tools/function_adapter.py +16 -127
  28. janito/tools/tool_base.py +114 -142
  29. janito/tools/tools_schema.py +6 -12
  30. {janito-3.3.0.dist-info → janito-3.4.0.dist-info}/METADATA +2 -1
  31. janito-3.4.0.dist-info/RECORD +264 -0
  32. janito/cli/cli_commands/check_tools.py +0 -212
  33. janito/plugin_system_backup_20250825_070018/__init__.py +0 -10
  34. janito/plugin_system_backup_20250825_070018/base.py +0 -155
  35. janito/plugin_system_backup_20250825_070018/core_loader_fixed.py +0 -149
  36. janito/plugins/__main__.py +0 -85
  37. janito/plugins/core/__init__.py +0 -7
  38. janito/plugins/core/codeanalyzer/__init__.py +0 -43
  39. janito/plugins/core/filemanager/__init__.py +0 -124
  40. janito/plugins/core/filemanager/tools/copy_file.py +0 -111
  41. janito/plugins/core/filemanager/tools/create_directory.py +0 -97
  42. janito/plugins/core/filemanager/tools/create_file.py +0 -111
  43. janito/plugins/core/filemanager/tools/replace_text_in_file.py +0 -270
  44. janito/plugins/core/imagedisplay/__init__.py +0 -14
  45. janito/plugins/core/imagedisplay/plugin.py +0 -51
  46. janito/plugins/core/imagedisplay/tools/__init__.py +0 -1
  47. janito/plugins/core/imagedisplay/tools/show_image.py +0 -83
  48. janito/plugins/core/system/__init__.py +0 -23
  49. janito/plugins/dev/__init__.py +0 -7
  50. janito/plugins/dev/pythondev/__init__.py +0 -37
  51. janito/plugins/dev/visualization/__init__.py +0 -23
  52. janito/plugins/example_plugin.py +0 -108
  53. janito/plugins/ui/__init__.py +0 -7
  54. janito/plugins/ui/userinterface/__init__.py +0 -16
  55. janito/plugins/ui/userinterface/tools/ask_user.py +0 -110
  56. janito/plugins/web/__init__.py +0 -7
  57. janito/plugins/web/webtools/__init__.py +0 -33
  58. janito/plugins/web/webtools/tools/fetch_url.py +0 -458
  59. janito/plugins_backup_20250825_070018/__init__.py +0 -36
  60. janito/plugins_backup_20250825_070018/builtin.py +0 -102
  61. janito/plugins_backup_20250825_070018/config.py +0 -84
  62. janito/plugins_backup_20250825_070018/core/__init__.py +0 -7
  63. janito/plugins_backup_20250825_070018/core/codeanalyzer/__init__.py +0 -43
  64. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/__init__.py +0 -1
  65. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/core.py +0 -122
  66. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/java_outline.py +0 -47
  67. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/markdown_outline.py +0 -14
  68. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/python_outline.py +0 -303
  69. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/search_outline.py +0 -36
  70. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/__init__.py +0 -1
  71. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/core.py +0 -205
  72. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/match_lines.py +0 -67
  73. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/pattern_utils.py +0 -73
  74. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/traverse_directory.py +0 -145
  75. janito/plugins_backup_20250825_070018/core/filemanager/__init__.py +0 -124
  76. janito/plugins_backup_20250825_070018/core/filemanager/tools/create_file.py +0 -87
  77. janito/plugins_backup_20250825_070018/core/filemanager/tools/delete_text_in_file.py +0 -135
  78. janito/plugins_backup_20250825_070018/core/filemanager/tools/find_files.py +0 -143
  79. janito/plugins_backup_20250825_070018/core/filemanager/tools/move_file.py +0 -131
  80. janito/plugins_backup_20250825_070018/core/filemanager/tools/read_files.py +0 -58
  81. janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_directory.py +0 -55
  82. janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_file.py +0 -58
  83. janito/plugins_backup_20250825_070018/core/filemanager/tools/replace_text_in_file.py +0 -270
  84. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/__init__.py +0 -1
  85. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/core.py +0 -114
  86. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/css_validator.py +0 -35
  87. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/html_validator.py +0 -100
  88. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/jinja2_validator.py +0 -50
  89. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/js_validator.py +0 -27
  90. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/json_validator.py +0 -6
  91. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/markdown_validator.py +0 -109
  92. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/ps1_validator.py +0 -32
  93. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/python_validator.py +0 -5
  94. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/xml_validator.py +0 -11
  95. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/yaml_validator.py +0 -6
  96. janito/plugins_backup_20250825_070018/core/filemanager/tools/view_file.py +0 -172
  97. janito/plugins_backup_20250825_070018/core/imagedisplay/__init__.py +0 -14
  98. janito/plugins_backup_20250825_070018/core/imagedisplay/plugin.py +0 -51
  99. janito/plugins_backup_20250825_070018/core/imagedisplay/tools/__init__.py +0 -1
  100. janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image.py +0 -83
  101. janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image_grid.py +0 -84
  102. janito/plugins_backup_20250825_070018/core/system/__init__.py +0 -23
  103. janito/plugins_backup_20250825_070018/core/system/tools/run_bash_command.py +0 -183
  104. janito/plugins_backup_20250825_070018/core/system/tools/run_powershell_command.py +0 -218
  105. janito/plugins_backup_20250825_070018/dev/__init__.py +0 -7
  106. janito/plugins_backup_20250825_070018/dev/pythondev/__init__.py +0 -37
  107. janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_code_run.py +0 -172
  108. janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_command_run.py +0 -171
  109. janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_file_run.py +0 -172
  110. janito/plugins_backup_20250825_070018/dev/visualization/__init__.py +0 -23
  111. janito/plugins_backup_20250825_070018/dev/visualization/tools/read_chart.py +0 -259
  112. janito/plugins_backup_20250825_070018/discovery.py +0 -289
  113. janito/plugins_backup_20250825_070018/example_plugin.py +0 -108
  114. janito/plugins_backup_20250825_070018/manager.py +0 -243
  115. janito/plugins_backup_20250825_070018/tools/__init__.py +0 -10
  116. janito/plugins_backup_20250825_070018/tools/copy_file.py +0 -87
  117. janito/plugins_backup_20250825_070018/tools/core_tools_plugin.py +0 -88
  118. janito/plugins_backup_20250825_070018/tools/create_directory.py +0 -70
  119. janito/plugins_backup_20250825_070018/tools/decorators.py +0 -19
  120. janito/plugins_backup_20250825_070018/tools/delete_text_in_file.py +0 -135
  121. janito/plugins_backup_20250825_070018/tools/find_files.py +0 -143
  122. janito/plugins_backup_20250825_070018/tools/get_file_outline/__init__.py +0 -7
  123. janito/plugins_backup_20250825_070018/tools/get_file_outline/core.py +0 -122
  124. janito/plugins_backup_20250825_070018/tools/get_file_outline/java_outline.py +0 -47
  125. janito/plugins_backup_20250825_070018/tools/get_file_outline/markdown_outline.py +0 -14
  126. janito/plugins_backup_20250825_070018/tools/get_file_outline/python_outline.py +0 -303
  127. janito/plugins_backup_20250825_070018/tools/get_file_outline/search_outline.py +0 -36
  128. janito/plugins_backup_20250825_070018/tools/move_file.py +0 -131
  129. janito/plugins_backup_20250825_070018/tools/open_html_in_browser.py +0 -51
  130. janito/plugins_backup_20250825_070018/tools/open_url.py +0 -37
  131. janito/plugins_backup_20250825_070018/tools/python_code_run.py +0 -172
  132. janito/plugins_backup_20250825_070018/tools/python_command_run.py +0 -171
  133. janito/plugins_backup_20250825_070018/tools/python_file_run.py +0 -172
  134. janito/plugins_backup_20250825_070018/tools/read_chart.py +0 -259
  135. janito/plugins_backup_20250825_070018/tools/read_files.py +0 -58
  136. janito/plugins_backup_20250825_070018/tools/remove_directory.py +0 -55
  137. janito/plugins_backup_20250825_070018/tools/remove_file.py +0 -58
  138. janito/plugins_backup_20250825_070018/tools/run_bash_command.py +0 -183
  139. janito/plugins_backup_20250825_070018/tools/run_powershell_command.py +0 -218
  140. janito/plugins_backup_20250825_070018/tools/search_text/__init__.py +0 -7
  141. janito/plugins_backup_20250825_070018/tools/search_text/core.py +0 -205
  142. janito/plugins_backup_20250825_070018/tools/search_text/match_lines.py +0 -67
  143. janito/plugins_backup_20250825_070018/tools/search_text/pattern_utils.py +0 -73
  144. janito/plugins_backup_20250825_070018/tools/search_text/traverse_directory.py +0 -145
  145. janito/plugins_backup_20250825_070018/tools/show_image_grid.py +0 -85
  146. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/__init__.py +0 -7
  147. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/core.py +0 -114
  148. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/css_validator.py +0 -35
  149. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/html_validator.py +0 -100
  150. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/jinja2_validator.py +0 -50
  151. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/js_validator.py +0 -27
  152. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/json_validator.py +0 -6
  153. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/markdown_validator.py +0 -109
  154. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/ps1_validator.py +0 -32
  155. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/python_validator.py +0 -5
  156. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/xml_validator.py +0 -11
  157. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/yaml_validator.py +0 -6
  158. janito/plugins_backup_20250825_070018/tools/view_file.py +0 -172
  159. janito/plugins_backup_20250825_070018/ui/__init__.py +0 -7
  160. janito/plugins_backup_20250825_070018/ui/userinterface/__init__.py +0 -16
  161. janito/plugins_backup_20250825_070018/ui/userinterface/tools/ask_user.py +0 -110
  162. janito/plugins_backup_20250825_070018/web/__init__.py +0 -7
  163. janito/plugins_backup_20250825_070018/web/webtools/__init__.py +0 -33
  164. janito/plugins_backup_20250825_070018/web/webtools/tools/fetch_url.py +0 -458
  165. janito/plugins_backup_20250825_070018/web/webtools/tools/open_html_in_browser.py +0 -51
  166. janito/plugins_backup_20250825_070018/web/webtools/tools/open_url.py +0 -37
  167. janito/tools/cli_initializer.py +0 -88
  168. janito/tools/initialize.py +0 -70
  169. janito-3.3.0.dist-info/RECORD +0 -400
  170. /janito/{plugins_backup_20250825_070018 → plugins}/auto_loader.py +0 -0
  171. /janito/{plugins_backup_20250825_070018 → plugins}/discovery_core.py +0 -0
  172. /janito/{plugins_backup_20250825_070018/core/filemanager/tools → tools/adapters/local}/copy_file.py +0 -0
  173. /janito/{plugins_backup_20250825_070018/core/filemanager/tools → tools/adapters/local}/create_directory.py +0 -0
  174. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/find_files.py +0 -0
  175. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/__init__.py +0 -0
  176. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/core.py +0 -0
  177. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/java_outline.py +0 -0
  178. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/markdown_outline.py +0 -0
  179. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/python_outline.py +0 -0
  180. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/search_outline.py +0 -0
  181. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/move_file.py +0 -0
  182. /janito/{plugins/web/webtools/tools → tools/adapters/local}/open_html_in_browser.py +0 -0
  183. /janito/{plugins/web/webtools/tools → tools/adapters/local}/open_url.py +0 -0
  184. /janito/{plugins/dev/pythondev/tools → tools/adapters/local}/python_code_run.py +0 -0
  185. /janito/{plugins/dev/pythondev/tools → tools/adapters/local}/python_command_run.py +0 -0
  186. /janito/{plugins/dev/pythondev/tools → tools/adapters/local}/python_file_run.py +0 -0
  187. /janito/{plugins/dev/visualization/tools → tools/adapters/local}/read_chart.py +0 -0
  188. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/read_files.py +0 -0
  189. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/remove_directory.py +0 -0
  190. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/remove_file.py +0 -0
  191. /janito/{plugins/core/system/tools → tools/adapters/local}/run_bash_command.py +0 -0
  192. /janito/{plugins/core/system/tools → tools/adapters/local}/run_powershell_command.py +0 -0
  193. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/__init__.py +0 -0
  194. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/core.py +0 -0
  195. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/match_lines.py +0 -0
  196. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/pattern_utils.py +0 -0
  197. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/traverse_directory.py +0 -0
  198. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/__init__.py +0 -0
  199. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/core.py +0 -0
  200. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/css_validator.py +0 -0
  201. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/html_validator.py +0 -0
  202. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/jinja2_validator.py +0 -0
  203. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/js_validator.py +0 -0
  204. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/json_validator.py +0 -0
  205. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/markdown_validator.py +0 -0
  206. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/ps1_validator.py +0 -0
  207. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/python_validator.py +0 -0
  208. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/xml_validator.py +0 -0
  209. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/yaml_validator.py +0 -0
  210. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/view_file.py +0 -0
  211. {janito-3.3.0.dist-info → janito-3.4.0.dist-info}/WHEEL +0 -0
  212. {janito-3.3.0.dist-info → janito-3.4.0.dist-info}/entry_points.txt +0 -0
  213. {janito-3.3.0.dist-info → janito-3.4.0.dist-info}/licenses/LICENSE +0 -0
  214. {janito-3.3.0.dist-info → janito-3.4.0.dist-info}/top_level.txt +0 -0
@@ -1,172 +0,0 @@
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.decorators import register_core_tool
9
- from janito.i18n import tr
10
-
11
-
12
- @register_core_tool
13
- class PythonCodeRun(ToolBase):
14
- """
15
- Tool to execute Python code by passing it to the interpreter via standard input (stdin).
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_code_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 (stdin mode) ...\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_stdin_stdout_",
46
- delete=False,
47
- encoding="utf-8",
48
- ) as stdout_file,
49
- tempfile.NamedTemporaryFile(
50
- mode="w+",
51
- prefix="python_stdin_stderr_",
52
- delete=False,
53
- encoding="utf-8",
54
- ) as stderr_file,
55
- ):
56
- process = subprocess.Popen(
57
- [sys.executable],
58
- stdin=subprocess.PIPE,
59
- stdout=subprocess.PIPE,
60
- stderr=subprocess.PIPE,
61
- text=True,
62
- bufsize=1,
63
- universal_newlines=True,
64
- encoding="utf-8",
65
- env={**os.environ, "PYTHONIOENCODING": "utf-8"},
66
- )
67
- stdout_lines, stderr_lines = self._stream_process_output(
68
- process, stdout_file, stderr_file, code
69
- )
70
- return_code = self._wait_for_process(process, timeout)
71
- if return_code is None:
72
- return tr(
73
- "Code timed out after {timeout} seconds.", timeout=timeout
74
- )
75
- stdout_file.flush()
76
- stderr_file.flush()
77
- if not silent:
78
- self.report_success(
79
- tr("✅ Return code {return_code}", return_code=return_code),
80
- ReportAction.EXECUTE,
81
- )
82
- return self._format_result(
83
- stdout_file.name, stderr_file.name, return_code
84
- )
85
- except Exception as e:
86
- self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
87
- return tr("Error running code via stdin: {error}", error=e)
88
-
89
- def _stream_process_output(self, process, stdout_file, stderr_file, code):
90
- stdout_lines = 0
91
- stderr_lines = 0
92
-
93
- def stream_output(stream, file_obj, report_func, count_func):
94
- nonlocal stdout_lines, stderr_lines
95
- for line in stream:
96
- file_obj.write(line)
97
- file_obj.flush()
98
- report_func(line.rstrip("\r\n"))
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
- process.stdin.write(code)
115
- process.stdin.close()
116
- stdout_thread.join()
117
- stderr_thread.join()
118
- return stdout_lines, stderr_lines
119
-
120
- def _wait_for_process(self, process, timeout):
121
- try:
122
- return process.wait(timeout=timeout)
123
- except subprocess.TimeoutExpired:
124
- process.kill()
125
- self.report_error(
126
- tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
127
- ReportAction.EXECUTE,
128
- )
129
- return None
130
-
131
- def _format_result(self, stdout_file_name, stderr_file_name, return_code):
132
- with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
133
- stdout_content = out_f.read()
134
- with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
135
- stderr_content = err_f.read()
136
- max_lines = 100
137
- stdout_lines = stdout_content.count("\n")
138
- stderr_lines = stderr_content.count("\n")
139
-
140
- def head_tail(text, n=10):
141
- lines = text.splitlines()
142
- if len(lines) <= 2 * n:
143
- return "\n".join(lines)
144
- return "\n".join(
145
- lines[:n]
146
- + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
147
- + lines[-n:]
148
- )
149
-
150
- if stdout_lines <= max_lines and stderr_lines <= max_lines:
151
- result = f"Return code: {return_code}\n--- python_code_run: STDOUT ---\n{stdout_content}"
152
- if stderr_content.strip():
153
- result += f"\n--- python_code_run: STDERR ---\n{stderr_content}"
154
- return result
155
- else:
156
- result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
157
- if stderr_lines > 0 and stderr_content.strip():
158
- result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
159
- result += f"returncode: {return_code}\n"
160
- result += (
161
- "--- python_code_run: STDOUT (head/tail) ---\n"
162
- + head_tail(stdout_content)
163
- + "\n"
164
- )
165
- if stderr_content.strip():
166
- result += (
167
- "--- python_code_run: STDERR (head/tail) ---\n"
168
- + head_tail(stderr_content)
169
- + "\n"
170
- )
171
- result += "Use the view_file tool to inspect the contents of these files when needed."
172
- return result
@@ -1,171 +0,0 @@
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.decorators import register_core_tool
9
- from janito.i18n import tr
10
-
11
-
12
- @register_core_tool
13
- class PythonCommandRun(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,172 +0,0 @@
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.decorators import register_core_tool
9
- from janito.i18n import tr
10
-
11
-
12
- @register_core_tool
13
- class PythonFileRun(ToolBase):
14
- """
15
- Tool to execute a specified Python script file.
16
-
17
- Args:
18
- path (str): Path to the Python script file to execute.
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_file_run"
28
-
29
- def run(self, path: str, timeout: int = 60, silent: bool = False) -> str:
30
- from janito.tools.path_utils import expand_path
31
-
32
- path = expand_path(path)
33
- if not silent:
34
- self.report_action(
35
- tr("🚀 Running: python {path}", path=path),
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_file_stdout_",
46
- delete=False,
47
- encoding="utf-8",
48
- ) as stdout_file,
49
- tempfile.NamedTemporaryFile(
50
- mode="w+",
51
- prefix="python_file_stderr_",
52
- delete=False,
53
- encoding="utf-8",
54
- ) as stderr_file,
55
- ):
56
- process = subprocess.Popen(
57
- [sys.executable, path],
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 file: {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
- # Always supply a default action for stdout/stderr reporting
98
- from janito.report_events import ReportAction
99
-
100
- report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
101
- if count_func == "stdout":
102
- stdout_lines += 1
103
- else:
104
- stderr_lines += 1
105
-
106
- stdout_thread = threading.Thread(
107
- target=stream_output,
108
- args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
109
- )
110
- stderr_thread = threading.Thread(
111
- target=stream_output,
112
- args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
113
- )
114
- stdout_thread.start()
115
- stderr_thread.start()
116
- stdout_thread.join()
117
- stderr_thread.join()
118
- return stdout_lines, stderr_lines
119
-
120
- def _wait_for_process(self, process, timeout):
121
- try:
122
- return process.wait(timeout=timeout)
123
- except subprocess.TimeoutExpired:
124
- process.kill()
125
- self.report_error(
126
- tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
127
- ReportAction.EXECUTE,
128
- )
129
- return None
130
-
131
- def _format_result(self, stdout_file_name, stderr_file_name, return_code):
132
- with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
133
- stdout_content = out_f.read()
134
- with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
135
- stderr_content = err_f.read()
136
- max_lines = 100
137
- stdout_lines = stdout_content.count("\n")
138
- stderr_lines = stderr_content.count("\n")
139
-
140
- def head_tail(text, n=10):
141
- lines = text.splitlines()
142
- if len(lines) <= 2 * n:
143
- return "\n".join(lines)
144
- return "\n".join(
145
- lines[:n]
146
- + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
147
- + lines[-n:]
148
- )
149
-
150
- if stdout_lines <= max_lines and stderr_lines <= max_lines:
151
- result = f"Return code: {return_code}\n--- python_file_run: STDOUT ---\n{stdout_content}"
152
- if stderr_content.strip():
153
- result += f"\n--- python_file_run: STDERR ---\n{stderr_content}"
154
- return result
155
- else:
156
- result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
157
- if stderr_lines > 0 and stderr_content.strip():
158
- result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
159
- result += f"returncode: {return_code}\n"
160
- result += (
161
- "--- python_file_run: STDOUT (head/tail) ---\n"
162
- + head_tail(stdout_content)
163
- + "\n"
164
- )
165
- if stderr_content.strip():
166
- result += (
167
- "--- python_file_run: STDERR (head/tail) ---\n"
168
- + head_tail(stderr_content)
169
- + "\n"
170
- )
171
- result += "Use the view_file tool to inspect the contents of these files when needed."
172
- return result