janito 3.2.0__py3-none-any.whl → 3.3.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 (166) hide show
  1. janito/README.md +0 -3
  2. janito/cli/chat_mode/bindings.py +0 -26
  3. janito/cli/chat_mode/session.py +1 -12
  4. janito/cli/chat_mode/shell/commands/security/allowed_sites.py +33 -47
  5. janito/cli/cli_commands/list_plugins.py +8 -13
  6. janito/cli/core/model_guesser.py +24 -40
  7. janito/cli/prompt_core.py +9 -20
  8. janito/i18n/it.py +46 -46
  9. janito/llm/agent.py +16 -32
  10. janito/llm/driver.py +0 -8
  11. janito/{plugin_system → plugin_system_backup_20250825_070018}/core_loader.py +3 -76
  12. janito/{plugin_system → plugin_system_backup_20250825_070018}/core_loader_fixed.py +3 -79
  13. janito/plugins/__init__.py +21 -29
  14. janito/plugins/__main__.py +85 -0
  15. janito/plugins/base.py +57 -0
  16. janito/plugins/builtin.py +1 -1
  17. janito/plugins/core/filemanager/tools/copy_file.py +25 -1
  18. janito/plugins/core/filemanager/tools/create_directory.py +28 -1
  19. janito/plugins/core/filemanager/tools/create_file.py +27 -3
  20. janito/plugins/core/filemanager/tools/delete_text_in_file.py +1 -0
  21. janito/plugins/core/imagedisplay/plugin.py +1 -1
  22. janito/plugins/core_loader.py +144 -0
  23. janito/plugins/discovery.py +3 -3
  24. janito/plugins/example_plugin.py +1 -1
  25. janito/plugins/manager.py +1 -1
  26. janito/plugins_backup_20250825_070018/__init__.py +36 -0
  27. janito/{plugins → plugins_backup_20250825_070018}/auto_loader.py +11 -12
  28. janito/plugins_backup_20250825_070018/builtin.py +102 -0
  29. janito/plugins_backup_20250825_070018/config.py +84 -0
  30. janito/plugins_backup_20250825_070018/core/__init__.py +7 -0
  31. janito/plugins_backup_20250825_070018/core/codeanalyzer/__init__.py +43 -0
  32. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/__init__.py +1 -0
  33. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/core.py +122 -0
  34. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/__init__.py +1 -0
  35. janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/core.py +205 -0
  36. janito/plugins_backup_20250825_070018/core/filemanager/__init__.py +124 -0
  37. janito/plugins_backup_20250825_070018/core/filemanager/tools/copy_file.py +87 -0
  38. janito/plugins_backup_20250825_070018/core/filemanager/tools/create_directory.py +70 -0
  39. janito/plugins_backup_20250825_070018/core/filemanager/tools/create_file.py +87 -0
  40. janito/plugins_backup_20250825_070018/core/filemanager/tools/delete_text_in_file.py +135 -0
  41. janito/plugins_backup_20250825_070018/core/filemanager/tools/find_files.py +143 -0
  42. janito/plugins_backup_20250825_070018/core/filemanager/tools/move_file.py +131 -0
  43. janito/plugins_backup_20250825_070018/core/filemanager/tools/read_files.py +58 -0
  44. janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_directory.py +55 -0
  45. janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_file.py +58 -0
  46. janito/plugins_backup_20250825_070018/core/filemanager/tools/replace_text_in_file.py +270 -0
  47. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/__init__.py +1 -0
  48. janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/core.py +114 -0
  49. janito/plugins_backup_20250825_070018/core/filemanager/tools/view_file.py +172 -0
  50. janito/plugins_backup_20250825_070018/core/imagedisplay/__init__.py +14 -0
  51. janito/plugins_backup_20250825_070018/core/imagedisplay/plugin.py +51 -0
  52. janito/plugins_backup_20250825_070018/core/imagedisplay/tools/__init__.py +1 -0
  53. janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image.py +83 -0
  54. janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image_grid.py +84 -0
  55. janito/plugins_backup_20250825_070018/core/system/__init__.py +23 -0
  56. janito/plugins_backup_20250825_070018/core/system/tools/run_bash_command.py +183 -0
  57. janito/plugins_backup_20250825_070018/core/system/tools/run_powershell_command.py +218 -0
  58. janito/plugins_backup_20250825_070018/core_adapter.py +55 -0
  59. janito/plugins_backup_20250825_070018/dev/__init__.py +7 -0
  60. janito/plugins_backup_20250825_070018/dev/pythondev/__init__.py +37 -0
  61. janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_code_run.py +172 -0
  62. janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_command_run.py +171 -0
  63. janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_file_run.py +172 -0
  64. janito/plugins_backup_20250825_070018/dev/visualization/__init__.py +23 -0
  65. janito/plugins_backup_20250825_070018/dev/visualization/tools/read_chart.py +259 -0
  66. janito/plugins_backup_20250825_070018/discovery.py +289 -0
  67. janito/{plugins → plugins_backup_20250825_070018}/discovery_core.py +9 -14
  68. janito/plugins_backup_20250825_070018/example_plugin.py +108 -0
  69. janito/plugins_backup_20250825_070018/manager.py +243 -0
  70. janito/{plugins → plugins_backup_20250825_070018}/tools/core_tools_plugin.py +10 -9
  71. janito/{plugins → plugins_backup_20250825_070018}/tools/create_file.py +2 -2
  72. janito/{plugins → plugins_backup_20250825_070018}/tools/delete_text_in_file.py +1 -0
  73. janito/plugins_backup_20250825_070018/tools/get_file_outline/java_outline.py +47 -0
  74. janito/plugins_backup_20250825_070018/tools/get_file_outline/markdown_outline.py +14 -0
  75. janito/plugins_backup_20250825_070018/tools/get_file_outline/python_outline.py +303 -0
  76. janito/plugins_backup_20250825_070018/tools/get_file_outline/search_outline.py +36 -0
  77. janito/plugins_backup_20250825_070018/tools/search_text/match_lines.py +67 -0
  78. janito/plugins_backup_20250825_070018/tools/search_text/pattern_utils.py +73 -0
  79. janito/plugins_backup_20250825_070018/tools/search_text/traverse_directory.py +145 -0
  80. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/css_validator.py +35 -0
  81. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/html_validator.py +100 -0
  82. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/jinja2_validator.py +50 -0
  83. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/js_validator.py +27 -0
  84. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/json_validator.py +6 -0
  85. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/markdown_validator.py +109 -0
  86. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/ps1_validator.py +32 -0
  87. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/python_validator.py +5 -0
  88. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/xml_validator.py +11 -0
  89. janito/plugins_backup_20250825_070018/tools/validate_file_syntax/yaml_validator.py +6 -0
  90. janito/plugins_backup_20250825_070018/ui/__init__.py +7 -0
  91. janito/plugins_backup_20250825_070018/ui/userinterface/__init__.py +16 -0
  92. janito/plugins_backup_20250825_070018/ui/userinterface/tools/ask_user.py +110 -0
  93. janito/plugins_backup_20250825_070018/web/__init__.py +7 -0
  94. janito/plugins_backup_20250825_070018/web/webtools/__init__.py +33 -0
  95. janito/plugins_backup_20250825_070018/web/webtools/tools/fetch_url.py +458 -0
  96. janito/plugins_backup_20250825_070018/web/webtools/tools/open_html_in_browser.py +51 -0
  97. janito/plugins_backup_20250825_070018/web/webtools/tools/open_url.py +37 -0
  98. janito/providers/__init__.py +0 -1
  99. janito/tools/base.py +31 -1
  100. janito/tools/cli_initializer.py +1 -1
  101. janito/tools/function_adapter.py +176 -0
  102. janito/tools/initialize.py +1 -1
  103. janito/tools/loop_protection_decorator.py +117 -114
  104. janito/tools/tool_base.py +142 -114
  105. janito/tools/tools_schema.py +12 -6
  106. {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/METADATA +1 -1
  107. {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/RECORD +160 -95
  108. janito/llm/cancellation_manager.py +0 -63
  109. janito/llm/enter_cancellation.py +0 -107
  110. janito/plugins/core_adapter.py +0 -131
  111. janito/providers/together/__init__.py +0 -1
  112. janito/providers/together/model_info.py +0 -69
  113. janito/providers/together/provider.py +0 -108
  114. /janito/{plugin_system → plugin_system_backup_20250825_070018}/__init__.py +0 -0
  115. /janito/{plugin_system → plugin_system_backup_20250825_070018}/base.py +0 -0
  116. /janito/{plugins → plugins_backup_20250825_070018}/auto_loader_fixed.py +0 -0
  117. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/java_outline.py +0 -0
  118. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/markdown_outline.py +0 -0
  119. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/python_outline.py +0 -0
  120. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/search_outline.py +0 -0
  121. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/search_text/match_lines.py +0 -0
  122. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/search_text/pattern_utils.py +0 -0
  123. /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/search_text/traverse_directory.py +0 -0
  124. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/css_validator.py +0 -0
  125. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/html_validator.py +0 -0
  126. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/jinja2_validator.py +0 -0
  127. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/js_validator.py +0 -0
  128. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/json_validator.py +0 -0
  129. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/markdown_validator.py +0 -0
  130. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/ps1_validator.py +0 -0
  131. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/python_validator.py +0 -0
  132. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/xml_validator.py +0 -0
  133. /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/yaml_validator.py +0 -0
  134. /janito/{plugins → plugins_backup_20250825_070018}/tools/__init__.py +0 -0
  135. /janito/{plugins → plugins_backup_20250825_070018}/tools/ask_user.py +0 -0
  136. /janito/{plugins → plugins_backup_20250825_070018}/tools/copy_file.py +0 -0
  137. /janito/{plugins → plugins_backup_20250825_070018}/tools/create_directory.py +0 -0
  138. /janito/{plugins → plugins_backup_20250825_070018}/tools/decorators.py +0 -0
  139. /janito/{plugins → plugins_backup_20250825_070018}/tools/fetch_url.py +0 -0
  140. /janito/{plugins → plugins_backup_20250825_070018}/tools/find_files.py +0 -0
  141. /janito/{plugins → plugins_backup_20250825_070018}/tools/get_file_outline/__init__.py +0 -0
  142. /janito/{plugins → plugins_backup_20250825_070018}/tools/get_file_outline/core.py +0 -0
  143. /janito/{plugins → plugins_backup_20250825_070018}/tools/move_file.py +0 -0
  144. /janito/{plugins → plugins_backup_20250825_070018}/tools/open_html_in_browser.py +0 -0
  145. /janito/{plugins → plugins_backup_20250825_070018}/tools/open_url.py +0 -0
  146. /janito/{plugins → plugins_backup_20250825_070018}/tools/python_code_run.py +0 -0
  147. /janito/{plugins → plugins_backup_20250825_070018}/tools/python_command_run.py +0 -0
  148. /janito/{plugins → plugins_backup_20250825_070018}/tools/python_file_run.py +0 -0
  149. /janito/{plugins → plugins_backup_20250825_070018}/tools/read_chart.py +0 -0
  150. /janito/{plugins → plugins_backup_20250825_070018}/tools/read_files.py +0 -0
  151. /janito/{plugins → plugins_backup_20250825_070018}/tools/remove_directory.py +0 -0
  152. /janito/{plugins → plugins_backup_20250825_070018}/tools/remove_file.py +0 -0
  153. /janito/{plugins → plugins_backup_20250825_070018}/tools/replace_text_in_file.py +0 -0
  154. /janito/{plugins → plugins_backup_20250825_070018}/tools/run_bash_command.py +0 -0
  155. /janito/{plugins → plugins_backup_20250825_070018}/tools/run_powershell_command.py +0 -0
  156. /janito/{plugins → plugins_backup_20250825_070018}/tools/search_text/__init__.py +0 -0
  157. /janito/{plugins → plugins_backup_20250825_070018}/tools/search_text/core.py +0 -0
  158. /janito/{plugins → plugins_backup_20250825_070018}/tools/show_image.py +0 -0
  159. /janito/{plugins → plugins_backup_20250825_070018}/tools/show_image_grid.py +0 -0
  160. /janito/{plugins → plugins_backup_20250825_070018}/tools/validate_file_syntax/__init__.py +0 -0
  161. /janito/{plugins → plugins_backup_20250825_070018}/tools/validate_file_syntax/core.py +0 -0
  162. /janito/{plugins → plugins_backup_20250825_070018}/tools/view_file.py +0 -0
  163. {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/WHEEL +0 -0
  164. {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/entry_points.txt +0 -0
  165. {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/licenses/LICENSE +0 -0
  166. {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,218 @@
1
+ from janito.tools.tool_base import ToolBase, ToolPermissions
2
+ from janito.report_events import ReportAction
3
+ from janito.tools.adapters.local.adapter import register_local_tool
4
+ from janito.i18n import tr
5
+ import subprocess
6
+ import os
7
+ from janito.tools.path_utils import expand_path
8
+ import tempfile
9
+ import threading
10
+
11
+
12
+ @register_local_tool
13
+ class RunPowershellCommandTool(ToolBase):
14
+ """
15
+ Execute a non-interactive command using the PowerShell shell and capture live output.
16
+ This tool explicitly invokes 'powershell.exe' (on Windows) or 'pwsh' (on other platforms if available).
17
+ All commands are automatically prepended with UTF-8 output encoding:
18
+ $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
19
+ For file output, it is recommended to use -Encoding utf8 in your PowerShell commands (e.g., Out-File -Encoding utf8) to ensure correct file encoding.
20
+
21
+ Args:
22
+ command (str): The PowerShell command to execute. This string is passed directly to PowerShell using the --Command argument (not as a script file).
23
+ timeout (int): Timeout in seconds for the command. Defaults to 60.
24
+ require_confirmation (bool): If True, require user confirmation before running. Defaults to False.
25
+ requires_user_input (bool): If True, warns that the command may require user input and might hang. Defaults to False. Non-interactive commands are preferred for automation and reliability.
26
+ silent (bool): If True, suppresses progress and status messages. Defaults to False.
27
+
28
+ Returns:
29
+ str: Output and status message, or file paths/line counts if output is large.
30
+ """
31
+
32
+ permissions = ToolPermissions(execute=True)
33
+ tool_name = "run_powershell_command"
34
+
35
+ def _confirm_and_warn(self, command, require_confirmation, requires_user_input):
36
+ if requires_user_input:
37
+ self.report_warning(
38
+ tr(
39
+ "⚠️ Warning: This command might be interactive, require user input, and might hang."
40
+ ),
41
+ ReportAction.EXECUTE,
42
+ )
43
+ if require_confirmation:
44
+ self.report_warning(
45
+ tr("⚠️ Confirmation requested, but no handler (auto-confirmed)."),
46
+ ReportAction.EXECUTE,
47
+ )
48
+ return True # Auto-confirm for now
49
+ return True
50
+
51
+ def _launch_process(self, shell_exe, command_with_encoding):
52
+ env = os.environ.copy()
53
+ env["PYTHONIOENCODING"] = "utf-8"
54
+ return subprocess.Popen(
55
+ [
56
+ shell_exe,
57
+ "-NoProfile",
58
+ "-ExecutionPolicy",
59
+ "Bypass",
60
+ "-Command",
61
+ command_with_encoding,
62
+ ],
63
+ stdout=subprocess.PIPE,
64
+ stderr=subprocess.PIPE,
65
+ text=True,
66
+ bufsize=1,
67
+ universal_newlines=True,
68
+ encoding="utf-8",
69
+ env=env,
70
+ )
71
+
72
+ def _stream_output(self, stream, file_obj, report_func, count_func, counter):
73
+ for line in stream:
74
+ file_obj.write(line)
75
+ file_obj.flush()
76
+ report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
77
+ if count_func == "stdout":
78
+ counter["stdout"] += 1
79
+ else:
80
+ counter["stderr"] += 1
81
+
82
+ def _format_result(
83
+ self, requires_user_input, return_code, stdout_file, stderr_file, max_lines=100
84
+ ):
85
+ warning_msg = ""
86
+ if requires_user_input:
87
+ warning_msg = tr(
88
+ "⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
89
+ )
90
+ with open(stdout_file.name, "r", encoding="utf-8", errors="replace") as out_f:
91
+ stdout_content = out_f.read()
92
+ with open(stderr_file.name, "r", encoding="utf-8", errors="replace") as err_f:
93
+ stderr_content = err_f.read()
94
+ stdout_lines = stdout_content.count("\n")
95
+ stderr_lines = stderr_content.count("\n")
96
+ if stdout_lines <= max_lines and stderr_lines <= max_lines:
97
+ result = warning_msg + tr(
98
+ "Return code: {return_code}\n--- STDOUT ---\n{stdout_content}",
99
+ return_code=return_code,
100
+ stdout_content=stdout_content,
101
+ )
102
+ if stderr_content.strip():
103
+ result += tr(
104
+ "\n--- STDERR ---\n{stderr_content}",
105
+ stderr_content=stderr_content,
106
+ )
107
+ return result
108
+ else:
109
+ result = warning_msg + tr(
110
+ "stdout_file: {stdout_file} (lines: {stdout_lines})\n",
111
+ stdout_file=stdout_file.name,
112
+ stdout_lines=stdout_lines,
113
+ )
114
+ if stderr_lines > 0 and stderr_content.strip():
115
+ result += tr(
116
+ "stderr_file: {stderr_file} (lines: {stderr_lines})\n",
117
+ stderr_file=stderr_file.name,
118
+ stderr_lines=stderr_lines,
119
+ )
120
+ result += tr(
121
+ "returncode: {return_code}\nUse the view_file tool to inspect the contents of these files when needed.",
122
+ return_code=return_code,
123
+ )
124
+ return result
125
+
126
+ def run(
127
+ self,
128
+ command: str,
129
+ timeout: int = 60,
130
+ require_confirmation: bool = False,
131
+ requires_user_input: bool = False,
132
+ silent: bool = False,
133
+ ) -> str:
134
+ if not command.strip():
135
+ self.report_warning(tr("ℹ️ Empty command provided."), ReportAction.EXECUTE)
136
+ return tr("Warning: Empty command provided. Operation skipped.")
137
+ encoding_prefix = "$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; "
138
+ command_with_encoding = encoding_prefix + command
139
+ if not silent:
140
+ self.report_action(
141
+ tr("🖥️ Running PowerShell command: {command} ...\n", command=command),
142
+ ReportAction.EXECUTE,
143
+ )
144
+ else:
145
+ self.report_action(tr("⚡ Executing..."), ReportAction.EXECUTE)
146
+ self._confirm_and_warn(command, require_confirmation, requires_user_input)
147
+ from janito.platform_discovery import PlatformDiscovery
148
+
149
+ pd = PlatformDiscovery()
150
+ shell_exe = "powershell.exe" if pd.is_windows() else "pwsh"
151
+ try:
152
+ with (
153
+ tempfile.NamedTemporaryFile(
154
+ mode="w+",
155
+ prefix="run_powershell_stdout_",
156
+ delete=False,
157
+ encoding="utf-8",
158
+ ) as stdout_file,
159
+ tempfile.NamedTemporaryFile(
160
+ mode="w+",
161
+ prefix="run_powershell_stderr_",
162
+ delete=False,
163
+ encoding="utf-8",
164
+ ) as stderr_file,
165
+ ):
166
+ process = self._launch_process(shell_exe, command_with_encoding)
167
+ counter = {"stdout": 0, "stderr": 0}
168
+ stdout_thread = threading.Thread(
169
+ target=self._stream_output,
170
+ args=(
171
+ process.stdout,
172
+ stdout_file,
173
+ self.report_stdout,
174
+ "stdout",
175
+ counter,
176
+ ),
177
+ )
178
+ stderr_thread = threading.Thread(
179
+ target=self._stream_output,
180
+ args=(
181
+ process.stderr,
182
+ stderr_file,
183
+ self.report_stderr,
184
+ "stderr",
185
+ counter,
186
+ ),
187
+ )
188
+ stdout_thread.start()
189
+ stderr_thread.start()
190
+ try:
191
+ return_code = process.wait(timeout=timeout)
192
+ except subprocess.TimeoutExpired:
193
+ process.kill()
194
+ self.report_error(
195
+ tr(
196
+ " ❌ Timed out after {timeout} seconds.",
197
+ timeout=timeout,
198
+ ),
199
+ ReportAction.EXECUTE,
200
+ )
201
+ return tr(
202
+ "Command timed out after {timeout} seconds.", timeout=timeout
203
+ )
204
+ stdout_thread.join()
205
+ stderr_thread.join()
206
+ stdout_file.flush()
207
+ stderr_file.flush()
208
+ if not silent:
209
+ self.report_success(
210
+ tr(" ✅ return code {return_code}", return_code=return_code),
211
+ ReportAction.EXECUTE,
212
+ )
213
+ return self._format_result(
214
+ requires_user_input, return_code, stdout_file, stderr_file
215
+ )
216
+ except Exception as e:
217
+ self.report_error(tr(" ❌ Error: {error}", error=e), ReportAction.EXECUTE)
218
+ return tr("Error running command: {error}", error=e)
@@ -0,0 +1,55 @@
1
+ """
2
+ Core plugin adapter for legacy plugin system.
3
+
4
+ This module provides proper Plugin class implementations for core plugins
5
+ that use the function-based approach instead of class-based.
6
+ """
7
+
8
+ from janito.plugin_system.base import Plugin, PluginMetadata
9
+ from typing import List, Type
10
+ from janito.tools.tool_base import ToolBase
11
+ from janito.tools.function_adapter import create_function_tool
12
+
13
+
14
+ class CorePluginAdapter(Plugin):
15
+ """Adapter for core plugins using function-based tools."""
16
+
17
+ def __init__(self, plugin_name: str, description: str, tools_module):
18
+ super().__init__()
19
+ self._plugin_name = plugin_name
20
+ self._description = description
21
+ self._tools_module = tools_module
22
+ self._tool_classes = []
23
+
24
+ # Set the metadata attribute that Plugin expects
25
+ self.metadata = self.get_metadata()
26
+
27
+ def get_metadata(self) -> PluginMetadata:
28
+ return PluginMetadata(
29
+ name=self._plugin_name,
30
+ version="1.0.0",
31
+ description=self._description,
32
+ author="Janito",
33
+ license="MIT",
34
+ )
35
+
36
+ def get_tools(self) -> List[Type[ToolBase]]:
37
+ return self._tool_classes
38
+
39
+ def initialize(self):
40
+ """Initialize the plugin by creating tool classes."""
41
+ # Get tools from the module
42
+ tools = getattr(self._tools_module, "__plugin_tools__", [])
43
+
44
+ self._tool_classes = []
45
+ for tool_func in tools:
46
+ if callable(tool_func):
47
+ tool_class = create_function_tool(tool_func)
48
+ self._tool_classes.append(tool_class)
49
+
50
+
51
+ def create_core_plugin(
52
+ plugin_name: str, description: str, tools_module
53
+ ) -> CorePluginAdapter:
54
+ """Create a core plugin adapter."""
55
+ return CorePluginAdapter(plugin_name, description, tools_module)
@@ -0,0 +1,7 @@
1
+ """
2
+ Development Plugin Package
3
+
4
+ Contains development-specific tools and utilities.
5
+ """
6
+
7
+ __all__ = ["pythondev", "visualization"]
@@ -0,0 +1,37 @@
1
+ """
2
+ Python Development Plugin
3
+
4
+ Python development and execution tools.
5
+ """
6
+
7
+ from typing import Optional
8
+
9
+
10
+ def python_code_run(code: str, timeout: int = 60) -> str:
11
+ """Execute Python code via stdin"""
12
+ return f"python_code_run(code='{len(code)} chars', timeout={timeout})"
13
+
14
+
15
+ python_code_run.tool_name = "python_code_run"
16
+
17
+
18
+ def python_command_run(code: str, timeout: int = 60) -> str:
19
+ """Execute Python with -c flag"""
20
+ return f"python_command_run(code='{len(code)} chars', timeout={timeout})"
21
+
22
+
23
+ python_command_run.tool_name = "python_command_run"
24
+
25
+
26
+ def python_file_run(path: str, timeout: int = 60) -> str:
27
+ """Run Python script files"""
28
+ return f"python_file_run(path='{path}', timeout={timeout})"
29
+
30
+
31
+ python_file_run.tool_name = "python_file_run"
32
+
33
+
34
+ # Plugin metadata
35
+ __plugin_name__ = "dev.pythondev"
36
+ __plugin_description__ = "Python development and execution"
37
+ __plugin_tools__ = [python_code_run, python_command_run, python_file_run]
@@ -0,0 +1,172 @@
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.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
+ 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
@@ -0,0 +1,171 @@
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.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
+ 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