janito 1.14.2__py3-none-any.whl → 2.0.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 (282) hide show
  1. janito/__init__.py +6 -1
  2. janito/__main__.py +1 -1
  3. janito/agent/setup_agent.py +139 -0
  4. janito/agent/templates/profiles/{system_prompt_template_base.txt.j2 → system_prompt_template_main.txt.j2} +1 -1
  5. janito/cli/__init__.py +9 -0
  6. janito/cli/chat_mode/bindings.py +37 -0
  7. janito/cli/chat_mode/chat_entry.py +23 -0
  8. janito/cli/chat_mode/prompt_style.py +19 -0
  9. janito/cli/chat_mode/session.py +272 -0
  10. janito/{shell/prompt/completer.py → cli/chat_mode/shell/autocomplete.py} +7 -6
  11. janito/cli/chat_mode/shell/commands/__init__.py +55 -0
  12. janito/cli/chat_mode/shell/commands/base.py +9 -0
  13. janito/cli/chat_mode/shell/commands/clear.py +12 -0
  14. janito/{shell → cli/chat_mode/shell}/commands/conversation_restart.py +34 -30
  15. janito/cli/chat_mode/shell/commands/edit.py +25 -0
  16. janito/cli/chat_mode/shell/commands/help.py +16 -0
  17. janito/cli/chat_mode/shell/commands/history_view.py +93 -0
  18. janito/cli/chat_mode/shell/commands/lang.py +25 -0
  19. janito/cli/chat_mode/shell/commands/last.py +137 -0
  20. janito/cli/chat_mode/shell/commands/livelogs.py +49 -0
  21. janito/cli/chat_mode/shell/commands/multi.py +51 -0
  22. janito/cli/chat_mode/shell/commands/prompt.py +64 -0
  23. janito/cli/chat_mode/shell/commands/role.py +36 -0
  24. janito/cli/chat_mode/shell/commands/session.py +40 -0
  25. janito/{shell → cli/chat_mode/shell}/commands/session_control.py +2 -2
  26. janito/cli/chat_mode/shell/commands/termweb_log.py +92 -0
  27. janito/cli/chat_mode/shell/commands/tools.py +32 -0
  28. janito/{shell → cli/chat_mode/shell}/commands/utility.py +4 -7
  29. janito/{shell → cli/chat_mode/shell}/commands/verbose.py +5 -5
  30. janito/cli/chat_mode/shell/session/__init__.py +1 -0
  31. janito/{shell → cli/chat_mode/shell}/session/manager.py +9 -1
  32. janito/cli/chat_mode/toolbar.py +90 -0
  33. janito/cli/cli_commands/list_models.py +35 -0
  34. janito/cli/cli_commands/list_providers.py +9 -0
  35. janito/cli/cli_commands/list_tools.py +53 -0
  36. janito/cli/cli_commands/model_selection.py +50 -0
  37. janito/cli/cli_commands/model_utils.py +84 -0
  38. janito/cli/cli_commands/set_api_key.py +19 -0
  39. janito/cli/cli_commands/show_config.py +51 -0
  40. janito/cli/cli_commands/show_system_prompt.py +62 -0
  41. janito/cli/config.py +28 -0
  42. janito/cli/console.py +3 -0
  43. janito/cli/core/__init__.py +4 -0
  44. janito/cli/core/event_logger.py +59 -0
  45. janito/cli/core/getters.py +31 -0
  46. janito/cli/core/runner.py +141 -0
  47. janito/cli/core/setters.py +174 -0
  48. janito/cli/core/unsetters.py +54 -0
  49. janito/cli/main.py +8 -196
  50. janito/cli/main_cli.py +312 -0
  51. janito/cli/prompt_core.py +230 -0
  52. janito/cli/prompt_handler.py +6 -0
  53. janito/cli/rich_terminal_reporter.py +101 -0
  54. janito/cli/single_shot_mode/__init__.py +6 -0
  55. janito/cli/single_shot_mode/handler.py +137 -0
  56. janito/cli/termweb_starter.py +73 -24
  57. janito/cli/utils.py +25 -0
  58. janito/cli/verbose_output.py +196 -0
  59. janito/config.py +5 -0
  60. janito/config_manager.py +110 -0
  61. janito/conversation_history.py +30 -0
  62. janito/{agent/tools_utils/dir_walk_utils.py → dir_walk_utils.py} +3 -2
  63. janito/driver_events.py +98 -0
  64. janito/drivers/anthropic/driver.py +113 -0
  65. janito/drivers/azure_openai/driver.py +36 -0
  66. janito/drivers/driver_registry.py +33 -0
  67. janito/drivers/google_genai/driver.py +54 -0
  68. janito/drivers/google_genai/schema_generator.py +67 -0
  69. janito/drivers/mistralai/driver.py +41 -0
  70. janito/drivers/openai/driver.py +334 -0
  71. janito/event_bus/__init__.py +2 -0
  72. janito/event_bus/bus.py +68 -0
  73. janito/event_bus/event.py +15 -0
  74. janito/event_bus/handler.py +31 -0
  75. janito/event_bus/queue_bus.py +57 -0
  76. janito/exceptions.py +23 -0
  77. janito/formatting_token.py +54 -0
  78. janito/i18n/pt.py +1 -0
  79. janito/llm/__init__.py +5 -0
  80. janito/llm/agent.py +443 -0
  81. janito/llm/auth.py +62 -0
  82. janito/llm/driver.py +239 -0
  83. janito/llm/driver_config.py +34 -0
  84. janito/llm/driver_config_builder.py +34 -0
  85. janito/llm/driver_input.py +12 -0
  86. janito/llm/message_parts.py +60 -0
  87. janito/llm/model.py +38 -0
  88. janito/llm/provider.py +187 -0
  89. janito/perf_singleton.py +3 -0
  90. janito/performance_collector.py +167 -0
  91. janito/provider_config.py +98 -0
  92. janito/provider_registry.py +152 -0
  93. janito/providers/__init__.py +7 -0
  94. janito/providers/anthropic/model_info.py +22 -0
  95. janito/providers/anthropic/provider.py +65 -0
  96. janito/providers/azure_openai/model_info.py +15 -0
  97. janito/providers/azure_openai/provider.py +72 -0
  98. janito/providers/deepseek/__init__.py +1 -0
  99. janito/providers/deepseek/model_info.py +16 -0
  100. janito/providers/deepseek/provider.py +91 -0
  101. janito/providers/google/__init__.py +1 -0
  102. janito/providers/google/model_info.py +40 -0
  103. janito/providers/google/provider.py +69 -0
  104. janito/providers/mistralai/model_info.py +37 -0
  105. janito/providers/mistralai/provider.py +69 -0
  106. janito/providers/openai/__init__.py +1 -0
  107. janito/providers/openai/model_info.py +137 -0
  108. janito/providers/openai/provider.py +107 -0
  109. janito/providers/openai/schema_generator.py +63 -0
  110. janito/providers/provider_static_info.py +21 -0
  111. janito/providers/registry.py +26 -0
  112. janito/report_events.py +38 -0
  113. janito/termweb/app.py +1 -1
  114. janito/tools/__init__.py +16 -0
  115. janito/tools/adapters/__init__.py +1 -0
  116. janito/tools/adapters/local/__init__.py +54 -0
  117. janito/tools/adapters/local/adapter.py +92 -0
  118. janito/{agent/tools → tools/adapters/local}/ask_user.py +30 -13
  119. janito/tools/adapters/local/copy_file.py +84 -0
  120. janito/{agent/tools → tools/adapters/local}/create_directory.py +11 -10
  121. janito/tools/adapters/local/create_file.py +82 -0
  122. janito/tools/adapters/local/delete_text_in_file.py +136 -0
  123. janito/{agent/tools → tools/adapters/local}/fetch_url.py +18 -19
  124. janito/tools/adapters/local/find_files.py +140 -0
  125. janito/tools/adapters/local/get_file_outline/core.py +151 -0
  126. janito/{agent/tools → tools/adapters/local}/get_file_outline/python_outline.py +125 -0
  127. janito/tools/adapters/local/get_file_outline/python_outline_v2.py +156 -0
  128. janito/{agent/tools → tools/adapters/local}/get_file_outline/search_outline.py +12 -7
  129. janito/{agent/tools → tools/adapters/local}/move_file.py +13 -9
  130. janito/{agent/tools → tools/adapters/local}/open_url.py +7 -5
  131. janito/tools/adapters/local/python_code_run.py +165 -0
  132. janito/tools/adapters/local/python_command_run.py +163 -0
  133. janito/tools/adapters/local/python_file_run.py +162 -0
  134. janito/{agent/tools → tools/adapters/local}/remove_directory.py +15 -9
  135. janito/{agent/tools → tools/adapters/local}/remove_file.py +17 -14
  136. janito/{agent/tools → tools/adapters/local}/replace_text_in_file.py +27 -22
  137. janito/tools/adapters/local/run_bash_command.py +176 -0
  138. janito/tools/adapters/local/run_powershell_command.py +219 -0
  139. janito/{agent/tools → tools/adapters/local}/search_text/core.py +32 -12
  140. janito/{agent/tools → tools/adapters/local}/search_text/match_lines.py +13 -4
  141. janito/{agent/tools → tools/adapters/local}/search_text/pattern_utils.py +12 -4
  142. janito/{agent/tools → tools/adapters/local}/search_text/traverse_directory.py +15 -2
  143. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/core.py +12 -11
  144. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/css_validator.py +1 -1
  145. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/html_validator.py +1 -1
  146. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/js_validator.py +1 -1
  147. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/json_validator.py +1 -1
  148. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/markdown_validator.py +1 -1
  149. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/ps1_validator.py +1 -1
  150. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/python_validator.py +1 -1
  151. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/xml_validator.py +1 -1
  152. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/yaml_validator.py +1 -1
  153. janito/{agent/tools/get_lines.py → tools/adapters/local/view_file.py} +45 -27
  154. janito/tools/inspect_registry.py +17 -0
  155. janito/tools/tool_base.py +105 -0
  156. janito/tools/tool_events.py +58 -0
  157. janito/tools/tool_run_exception.py +12 -0
  158. janito/{agent → tools}/tool_use_tracker.py +2 -4
  159. janito/{agent/tools_utils/utils.py → tools/tool_utils.py} +18 -9
  160. janito/tools/tools_adapter.py +207 -0
  161. janito/tools/tools_schema.py +104 -0
  162. janito/utils.py +11 -0
  163. janito/version.py +4 -0
  164. janito-2.0.0.dist-info/METADATA +232 -0
  165. janito-2.0.0.dist-info/RECORD +180 -0
  166. janito/agent/__init__.py +0 -0
  167. janito/agent/api_exceptions.py +0 -4
  168. janito/agent/config.py +0 -147
  169. janito/agent/config_defaults.py +0 -12
  170. janito/agent/config_utils.py +0 -0
  171. janito/agent/content_handler.py +0 -0
  172. janito/agent/conversation.py +0 -238
  173. janito/agent/conversation_api.py +0 -306
  174. janito/agent/conversation_exceptions.py +0 -18
  175. janito/agent/conversation_tool_calls.py +0 -39
  176. janito/agent/conversation_ui.py +0 -17
  177. janito/agent/event.py +0 -24
  178. janito/agent/event_dispatcher.py +0 -24
  179. janito/agent/event_handler_protocol.py +0 -5
  180. janito/agent/event_system.py +0 -15
  181. janito/agent/llm_conversation_history.py +0 -82
  182. janito/agent/message_handler.py +0 -20
  183. janito/agent/message_handler_protocol.py +0 -5
  184. janito/agent/openai_client.py +0 -149
  185. janito/agent/openai_schema_generator.py +0 -187
  186. janito/agent/profile_manager.py +0 -96
  187. janito/agent/queued_message_handler.py +0 -50
  188. janito/agent/rich_live.py +0 -32
  189. janito/agent/rich_message_handler.py +0 -115
  190. janito/agent/runtime_config.py +0 -36
  191. janito/agent/test_handler_protocols.py +0 -47
  192. janito/agent/test_openai_schema_generator.py +0 -93
  193. janito/agent/tests/__init__.py +0 -1
  194. janito/agent/tool_base.py +0 -63
  195. janito/agent/tool_executor.py +0 -122
  196. janito/agent/tool_registry.py +0 -49
  197. janito/agent/tools/__init__.py +0 -47
  198. janito/agent/tools/create_file.py +0 -59
  199. janito/agent/tools/delete_text_in_file.py +0 -97
  200. janito/agent/tools/find_files.py +0 -106
  201. janito/agent/tools/get_file_outline/core.py +0 -81
  202. janito/agent/tools/present_choices.py +0 -64
  203. janito/agent/tools/python_command_runner.py +0 -201
  204. janito/agent/tools/python_file_runner.py +0 -199
  205. janito/agent/tools/python_stdin_runner.py +0 -208
  206. janito/agent/tools/replace_file.py +0 -72
  207. janito/agent/tools/run_bash_command.py +0 -218
  208. janito/agent/tools/run_powershell_command.py +0 -251
  209. janito/agent/tools_utils/__init__.py +0 -1
  210. janito/agent/tools_utils/action_type.py +0 -7
  211. janito/agent/tools_utils/test_gitignore_utils.py +0 -46
  212. janito/cli/_livereload_log_utils.py +0 -13
  213. janito/cli/_print_config.py +0 -96
  214. janito/cli/_termweb_log_utils.py +0 -17
  215. janito/cli/_utils.py +0 -9
  216. janito/cli/arg_parser.py +0 -272
  217. janito/cli/cli_main.py +0 -281
  218. janito/cli/config_commands.py +0 -211
  219. janito/cli/config_runner.py +0 -35
  220. janito/cli/formatting_runner.py +0 -12
  221. janito/cli/livereload_starter.py +0 -60
  222. janito/cli/logging_setup.py +0 -38
  223. janito/cli/one_shot.py +0 -80
  224. janito/livereload/app.py +0 -25
  225. janito/rich_utils.py +0 -59
  226. janito/shell/__init__.py +0 -0
  227. janito/shell/commands/__init__.py +0 -61
  228. janito/shell/commands/config.py +0 -22
  229. janito/shell/commands/edit.py +0 -24
  230. janito/shell/commands/history_view.py +0 -18
  231. janito/shell/commands/lang.py +0 -19
  232. janito/shell/commands/livelogs.py +0 -42
  233. janito/shell/commands/prompt.py +0 -62
  234. janito/shell/commands/termweb_log.py +0 -94
  235. janito/shell/commands/tools.py +0 -26
  236. janito/shell/commands/track.py +0 -36
  237. janito/shell/main.py +0 -326
  238. janito/shell/prompt/load_prompt.py +0 -57
  239. janito/shell/prompt/session_setup.py +0 -57
  240. janito/shell/session/config.py +0 -109
  241. janito/shell/session/history.py +0 -0
  242. janito/shell/ui/interactive.py +0 -226
  243. janito/termweb/static/editor.css +0 -158
  244. janito/termweb/static/editor.css.bak +0 -145
  245. janito/termweb/static/editor.html +0 -46
  246. janito/termweb/static/editor.html.bak +0 -46
  247. janito/termweb/static/editor.js +0 -265
  248. janito/termweb/static/editor.js.bak +0 -259
  249. janito/termweb/static/explorer.html.bak +0 -59
  250. janito/termweb/static/favicon.ico +0 -0
  251. janito/termweb/static/favicon.ico.bak +0 -0
  252. janito/termweb/static/index.html +0 -53
  253. janito/termweb/static/index.html.bak +0 -54
  254. janito/termweb/static/index.html.bak.bak +0 -175
  255. janito/termweb/static/landing.html.bak +0 -36
  256. janito/termweb/static/termicon.svg +0 -1
  257. janito/termweb/static/termweb.css +0 -214
  258. janito/termweb/static/termweb.css.bak +0 -237
  259. janito/termweb/static/termweb.js +0 -162
  260. janito/termweb/static/termweb.js.bak +0 -168
  261. janito/termweb/static/termweb.js.bak.bak +0 -157
  262. janito/termweb/static/termweb_quickopen.js +0 -135
  263. janito/termweb/static/termweb_quickopen.js.bak +0 -125
  264. janito/tests/test_rich_utils.py +0 -44
  265. janito/web/__init__.py +0 -0
  266. janito/web/__main__.py +0 -25
  267. janito/web/app.py +0 -145
  268. janito-1.14.2.dist-info/METADATA +0 -306
  269. janito-1.14.2.dist-info/RECORD +0 -162
  270. janito-1.14.2.dist-info/licenses/LICENSE +0 -21
  271. /janito/{shell → cli/chat_mode/shell}/input_history.py +0 -0
  272. /janito/{shell/commands/session.py → cli/chat_mode/shell/session/history.py} +0 -0
  273. /janito/{agent/tools_utils/formatting.py → formatting.py} +0 -0
  274. /janito/{agent/tools_utils/gitignore_utils.py → gitignore_utils.py} +0 -0
  275. /janito/{agent/platform_discovery.py → platform_discovery.py} +0 -0
  276. /janito/{agent/tools → tools/adapters/local}/get_file_outline/__init__.py +0 -0
  277. /janito/{agent/tools → tools/adapters/local}/get_file_outline/markdown_outline.py +0 -0
  278. /janito/{agent/tools → tools/adapters/local}/search_text/__init__.py +0 -0
  279. /janito/{agent/tools → tools/adapters/local}/validate_file_syntax/__init__.py +0 -0
  280. {janito-1.14.2.dist-info → janito-2.0.0.dist-info}/WHEEL +0 -0
  281. {janito-1.14.2.dist-info → janito-2.0.0.dist-info}/entry_points.txt +0 -0
  282. {janito-1.14.2.dist-info → janito-2.0.0.dist-info}/top_level.txt +0 -0
@@ -1,24 +1,29 @@
1
- from janito.agent.tool_base import ToolBase
2
- from janito.agent.tools_utils.action_type import ActionType
3
- from janito.agent.tool_registry import register_tool
1
+ from janito.tools.tool_base import ToolBase
2
+ from janito.report_events import ReportAction
4
3
 
5
4
 
6
- @register_tool(name="search_outline")
7
5
  class SearchOutlineTool(ToolBase):
8
6
  """
9
7
  Tool for searching outlines in files.
8
+
9
+ Args:
10
+ file_path (str): Path to the file for which to generate an outline.
11
+ Returns:
12
+ str: Outline search result or status message.
10
13
  """
11
14
 
15
+ tool_name = "search_outline"
16
+
12
17
  def run(self, file_path: str) -> str:
13
- from janito.agent.tools_utils.utils import display_path
18
+ from janito.tools.tool_utils import display_path
14
19
  from janito.i18n import tr
15
20
 
16
- self.report_info(
17
- ActionType.READ,
21
+ self.report_action(
18
22
  tr(
19
23
  "🔍 Searching for outline in '{disp_path}'",
20
24
  disp_path=display_path(file_path),
21
25
  ),
26
+ ReportAction.READ,
22
27
  )
23
28
  # ... rest of implementation ...
24
29
  # Example warnings and successes:
@@ -1,13 +1,13 @@
1
1
  import os
2
2
  import shutil
3
- from janito.agent.tool_registry import register_tool
4
- from janito.agent.tools_utils.utils import display_path
5
- from janito.agent.tool_base import ToolBase
6
- from janito.agent.tools_utils.action_type import ActionType
3
+ from janito.tools.adapters.local.adapter import register_local_tool
4
+ from janito.tools.tool_utils import display_path
5
+ from janito.tools.tool_base import ToolBase
6
+ from janito.report_events import ReportAction
7
7
  from janito.i18n import tr
8
8
 
9
9
 
10
- @register_tool(name="move_file")
10
+ @register_local_tool
11
11
  class MoveFileTool(ToolBase):
12
12
  """
13
13
  Move a file or directory from src_path to dest_path.
@@ -21,6 +21,8 @@ class MoveFileTool(ToolBase):
21
21
  str: Status message indicating the result.
22
22
  """
23
23
 
24
+ tool_name = "move_file"
25
+
24
26
  def run(
25
27
  self,
26
28
  src_path: str,
@@ -47,13 +49,13 @@ class MoveFileTool(ToolBase):
47
49
  return err_msg
48
50
 
49
51
  try:
50
- self.report_info(
51
- ActionType.WRITE,
52
+ self.report_action(
52
53
  tr(
53
54
  "📝 Moving from '{disp_src}' to '{disp_dest}' ...",
54
55
  disp_src=disp_src,
55
56
  disp_dest=disp_dest,
56
57
  ),
58
+ ReportAction.CREATE,
57
59
  )
58
60
  shutil.move(src, dest)
59
61
  self.report_success(tr("✅ Move complete."))
@@ -107,7 +109,8 @@ class MoveFileTool(ToolBase):
107
109
  tr(
108
110
  "❗ Destination '{disp_dest}' exists and overwrite is False.",
109
111
  disp_dest=disp_dest,
110
- )
112
+ ),
113
+ ReportAction.MOVE,
111
114
  )
112
115
  return None, tr(
113
116
  "❗ Destination '{disp_dest}' already exists and overwrite is False.",
@@ -127,7 +130,8 @@ class MoveFileTool(ToolBase):
127
130
  shutil.rmtree(dest)
128
131
  except Exception as e:
129
132
  self.report_error(
130
- tr("❌ Error removing destination before move: {error}", error=e)
133
+ tr("❌ Error removing destination before move: {error}", error=e),
134
+ ReportAction.MOVE,
131
135
  )
132
136
  return None, tr(
133
137
  "❌ Error removing destination before move: {error}", error=e
@@ -1,11 +1,11 @@
1
1
  import webbrowser
2
- from janito.agent.tool_registry import register_tool
3
- from janito.agent.tool_base import ToolBase
4
- from janito.agent.tools_utils.action_type import ActionType
2
+ from janito.tools.adapters.local.adapter import register_local_tool
3
+ from janito.tools.tool_base import ToolBase
4
+ from janito.report_events import ReportAction
5
5
  from janito.i18n import tr
6
6
 
7
7
 
8
- @register_tool(name="open_url")
8
+ @register_local_tool
9
9
  class OpenUrlTool(ToolBase):
10
10
  """
11
11
  Open the supplied URL in the default web browser.
@@ -15,11 +15,13 @@ class OpenUrlTool(ToolBase):
15
15
  str: Status message indicating the result.
16
16
  """
17
17
 
18
+ tool_name = "open_url"
19
+
18
20
  def run(self, url: str) -> str:
19
21
  if not url.strip():
20
22
  self.report_warning(tr("ℹ️ Empty URL provided."))
21
23
  return tr("Warning: Empty URL provided. Operation skipped.")
22
- self.report_info(ActionType.READ, tr("🌐 Opening URL '{url}' ...", url=url))
24
+ self.report_action(tr("🌐 Opening URL '{url}' ...", url=url), ReportAction.READ)
23
25
  try:
24
26
  webbrowser.open(url)
25
27
  except Exception as err:
@@ -0,0 +1,165 @@
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
+ try:
36
+ with (
37
+ tempfile.NamedTemporaryFile(
38
+ mode="w+",
39
+ prefix="python_stdin_stdout_",
40
+ delete=False,
41
+ encoding="utf-8",
42
+ ) as stdout_file,
43
+ tempfile.NamedTemporaryFile(
44
+ mode="w+",
45
+ prefix="python_stdin_stderr_",
46
+ delete=False,
47
+ encoding="utf-8",
48
+ ) as stderr_file,
49
+ ):
50
+ process = subprocess.Popen(
51
+ [sys.executable],
52
+ stdin=subprocess.PIPE,
53
+ stdout=subprocess.PIPE,
54
+ stderr=subprocess.PIPE,
55
+ text=True,
56
+ bufsize=1,
57
+ universal_newlines=True,
58
+ encoding="utf-8",
59
+ env={**os.environ, "PYTHONIOENCODING": "utf-8"},
60
+ )
61
+ stdout_lines, stderr_lines = self._stream_process_output(
62
+ process, stdout_file, stderr_file, code
63
+ )
64
+ return_code = self._wait_for_process(process, timeout)
65
+ if return_code is None:
66
+ return tr(
67
+ "Code timed out after {timeout} seconds.", timeout=timeout
68
+ )
69
+ stdout_file.flush()
70
+ stderr_file.flush()
71
+ self.report_success(
72
+ tr("✅ Return code {return_code}", return_code=return_code),
73
+ ReportAction.EXECUTE,
74
+ )
75
+ return self._format_result(
76
+ stdout_file.name, stderr_file.name, return_code
77
+ )
78
+ except Exception as e:
79
+ self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
80
+ return tr("Error running code via stdin: {error}", error=e)
81
+
82
+ def _stream_process_output(self, process, stdout_file, stderr_file, code):
83
+ stdout_lines = 0
84
+ stderr_lines = 0
85
+
86
+ def stream_output(stream, file_obj, report_func, count_func):
87
+ nonlocal stdout_lines, stderr_lines
88
+ for line in stream:
89
+ file_obj.write(line)
90
+ file_obj.flush()
91
+ report_func(line.rstrip("\r\n"))
92
+ if count_func == "stdout":
93
+ stdout_lines += 1
94
+ else:
95
+ stderr_lines += 1
96
+
97
+ stdout_thread = threading.Thread(
98
+ target=stream_output,
99
+ args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
100
+ )
101
+ stderr_thread = threading.Thread(
102
+ target=stream_output,
103
+ args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
104
+ )
105
+ stdout_thread.start()
106
+ stderr_thread.start()
107
+ process.stdin.write(code)
108
+ process.stdin.close()
109
+ stdout_thread.join()
110
+ stderr_thread.join()
111
+ return stdout_lines, stderr_lines
112
+
113
+ def _wait_for_process(self, process, timeout):
114
+ try:
115
+ return process.wait(timeout=timeout)
116
+ except subprocess.TimeoutExpired:
117
+ process.kill()
118
+ self.report_error(
119
+ tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
120
+ ReportAction.EXECUTE,
121
+ )
122
+ return None
123
+
124
+ def _format_result(self, stdout_file_name, stderr_file_name, return_code):
125
+ with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
126
+ stdout_content = out_f.read()
127
+ with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
128
+ stderr_content = err_f.read()
129
+ max_lines = 100
130
+ stdout_lines = stdout_content.count("\n")
131
+ stderr_lines = stderr_content.count("\n")
132
+
133
+ def head_tail(text, n=10):
134
+ lines = text.splitlines()
135
+ if len(lines) <= 2 * n:
136
+ return "\n".join(lines)
137
+ return "\n".join(
138
+ lines[:n]
139
+ + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
140
+ + lines[-n:]
141
+ )
142
+
143
+ if stdout_lines <= max_lines and stderr_lines <= max_lines:
144
+ result = f"Return code: {return_code}\n--- python_code_run: STDOUT ---\n{stdout_content}"
145
+ if stderr_content.strip():
146
+ result += f"\n--- python_code_run: STDERR ---\n{stderr_content}"
147
+ return result
148
+ else:
149
+ result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
150
+ if stderr_lines > 0 and stderr_content.strip():
151
+ result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
152
+ result += f"returncode: {return_code}\n"
153
+ result += (
154
+ "--- python_code_run: STDOUT (head/tail) ---\n"
155
+ + head_tail(stdout_content)
156
+ + "\n"
157
+ )
158
+ if stderr_content.strip():
159
+ result += (
160
+ "--- python_code_run: STDERR (head/tail) ---\n"
161
+ + head_tail(stderr_content)
162
+ + "\n"
163
+ )
164
+ result += "Use the view_file tool to inspect the contents of these files when needed."
165
+ return result
@@ -0,0 +1,163 @@
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
+ try:
35
+ with (
36
+ tempfile.NamedTemporaryFile(
37
+ mode="w+",
38
+ prefix="python_cmd_stdout_",
39
+ delete=False,
40
+ encoding="utf-8",
41
+ ) as stdout_file,
42
+ tempfile.NamedTemporaryFile(
43
+ mode="w+",
44
+ prefix="python_cmd_stderr_",
45
+ delete=False,
46
+ encoding="utf-8",
47
+ ) as stderr_file,
48
+ ):
49
+ process = subprocess.Popen(
50
+ [sys.executable, "-c", code],
51
+ stdout=subprocess.PIPE,
52
+ stderr=subprocess.PIPE,
53
+ text=True,
54
+ bufsize=1,
55
+ universal_newlines=True,
56
+ encoding="utf-8",
57
+ env={**os.environ, "PYTHONIOENCODING": "utf-8"},
58
+ )
59
+ stdout_lines, stderr_lines = self._stream_process_output(
60
+ process, stdout_file, stderr_file
61
+ )
62
+ return_code = self._wait_for_process(process, timeout)
63
+ if return_code is None:
64
+ return tr(
65
+ "Code timed out after {timeout} seconds.", timeout=timeout
66
+ )
67
+ stdout_file.flush()
68
+ stderr_file.flush()
69
+ self.report_success(
70
+ tr("✅ Return code {return_code}", return_code=return_code),
71
+ ReportAction.EXECUTE,
72
+ )
73
+ return self._format_result(
74
+ stdout_file.name, stderr_file.name, return_code
75
+ )
76
+ except Exception as e:
77
+ self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
78
+ return tr("Error running code: {error}", error=e)
79
+
80
+ def _stream_process_output(self, process, stdout_file, stderr_file):
81
+ stdout_lines = 0
82
+ stderr_lines = 0
83
+
84
+ def stream_output(stream, file_obj, report_func, count_func):
85
+ nonlocal stdout_lines, stderr_lines
86
+ for line in stream:
87
+ file_obj.write(line)
88
+ file_obj.flush()
89
+ from janito.tools.tool_base import ReportAction
90
+
91
+ report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
92
+ if count_func == "stdout":
93
+ stdout_lines += 1
94
+ else:
95
+ stderr_lines += 1
96
+
97
+ stdout_thread = threading.Thread(
98
+ target=stream_output,
99
+ args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
100
+ )
101
+ stderr_thread = threading.Thread(
102
+ target=stream_output,
103
+ args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
104
+ )
105
+ stdout_thread.start()
106
+ stderr_thread.start()
107
+ stdout_thread.join()
108
+ stderr_thread.join()
109
+ return stdout_lines, stderr_lines
110
+
111
+ def _wait_for_process(self, process, timeout):
112
+ try:
113
+ return process.wait(timeout=timeout)
114
+ except subprocess.TimeoutExpired:
115
+ process.kill()
116
+ self.report_error(
117
+ tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
118
+ ReportAction.EXECUTE,
119
+ )
120
+ return None
121
+
122
+ def _format_result(self, stdout_file_name, stderr_file_name, return_code):
123
+ with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
124
+ stdout_content = out_f.read()
125
+ with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
126
+ stderr_content = err_f.read()
127
+ max_lines = 100
128
+ stdout_lines = stdout_content.count("\n")
129
+ stderr_lines = stderr_content.count("\n")
130
+
131
+ def head_tail(text, n=10):
132
+ lines = text.splitlines()
133
+ if len(lines) <= 2 * n:
134
+ return "\n".join(lines)
135
+ return "\n".join(
136
+ lines[:n]
137
+ + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
138
+ + lines[-n:]
139
+ )
140
+
141
+ if stdout_lines <= max_lines and stderr_lines <= max_lines:
142
+ result = f"Return code: {return_code}\n--- python_command_run: STDOUT ---\n{stdout_content}"
143
+ if stderr_content.strip():
144
+ result += f"\n--- python_command_run: STDERR ---\n{stderr_content}"
145
+ return result
146
+ else:
147
+ result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
148
+ if stderr_lines > 0 and stderr_content.strip():
149
+ result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
150
+ result += f"returncode: {return_code}\n"
151
+ result += (
152
+ "--- python_command_run: STDOUT (head/tail) ---\n"
153
+ + head_tail(stdout_content)
154
+ + "\n"
155
+ )
156
+ if stderr_content.strip():
157
+ result += (
158
+ "--- python_command_run: STDERR (head/tail) ---\n"
159
+ + head_tail(stderr_content)
160
+ + "\n"
161
+ )
162
+ result += "Use the view_file tool to inspect the contents of these files when needed."
163
+ return result
@@ -0,0 +1,162 @@
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 PythonFileRunTool(ToolBase):
14
+ """
15
+ Tool to execute a specified Python script file.
16
+
17
+ Parameters:
18
+ file_path (str): Path to the Python script file to execute.
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_file_run"
26
+
27
+ def run(self, file_path: str, timeout: int = 60) -> str:
28
+ self.report_action(
29
+ tr("🚀 Running: python {file_path}", file_path=file_path),
30
+ ReportAction.EXECUTE,
31
+ )
32
+ try:
33
+ with (
34
+ tempfile.NamedTemporaryFile(
35
+ mode="w+",
36
+ prefix="python_file_stdout_",
37
+ delete=False,
38
+ encoding="utf-8",
39
+ ) as stdout_file,
40
+ tempfile.NamedTemporaryFile(
41
+ mode="w+",
42
+ prefix="python_file_stderr_",
43
+ delete=False,
44
+ encoding="utf-8",
45
+ ) as stderr_file,
46
+ ):
47
+ process = subprocess.Popen(
48
+ [sys.executable, file_path],
49
+ stdout=subprocess.PIPE,
50
+ stderr=subprocess.PIPE,
51
+ text=True,
52
+ bufsize=1,
53
+ universal_newlines=True,
54
+ encoding="utf-8",
55
+ env={**os.environ, "PYTHONIOENCODING": "utf-8"},
56
+ )
57
+ stdout_lines, stderr_lines = self._stream_process_output(
58
+ process, stdout_file, stderr_file
59
+ )
60
+ return_code = self._wait_for_process(process, timeout)
61
+ if return_code is None:
62
+ return tr(
63
+ "Code timed out after {timeout} seconds.", timeout=timeout
64
+ )
65
+ stdout_file.flush()
66
+ stderr_file.flush()
67
+ self.report_success(
68
+ tr("✅ Return code {return_code}", return_code=return_code),
69
+ ReportAction.EXECUTE,
70
+ )
71
+ return self._format_result(
72
+ stdout_file.name, stderr_file.name, return_code
73
+ )
74
+ except Exception as e:
75
+ self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
76
+ return tr("Error running file: {error}", error=e)
77
+
78
+ def _stream_process_output(self, process, stdout_file, stderr_file):
79
+ stdout_lines = 0
80
+ stderr_lines = 0
81
+
82
+ def stream_output(stream, file_obj, report_func, count_func):
83
+ nonlocal stdout_lines, stderr_lines
84
+ for line in stream:
85
+ file_obj.write(line)
86
+ file_obj.flush()
87
+ # Always supply a default action for stdout/stderr reporting
88
+ from janito.report_events import ReportAction
89
+
90
+ report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
91
+ if count_func == "stdout":
92
+ stdout_lines += 1
93
+ else:
94
+ stderr_lines += 1
95
+
96
+ stdout_thread = threading.Thread(
97
+ target=stream_output,
98
+ args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
99
+ )
100
+ stderr_thread = threading.Thread(
101
+ target=stream_output,
102
+ args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
103
+ )
104
+ stdout_thread.start()
105
+ stderr_thread.start()
106
+ stdout_thread.join()
107
+ stderr_thread.join()
108
+ return stdout_lines, stderr_lines
109
+
110
+ def _wait_for_process(self, process, timeout):
111
+ try:
112
+ return process.wait(timeout=timeout)
113
+ except subprocess.TimeoutExpired:
114
+ process.kill()
115
+ self.report_error(
116
+ tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
117
+ ReportAction.EXECUTE,
118
+ )
119
+ return None
120
+
121
+ def _format_result(self, stdout_file_name, stderr_file_name, return_code):
122
+ with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
123
+ stdout_content = out_f.read()
124
+ with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
125
+ stderr_content = err_f.read()
126
+ max_lines = 100
127
+ stdout_lines = stdout_content.count("\n")
128
+ stderr_lines = stderr_content.count("\n")
129
+
130
+ def head_tail(text, n=10):
131
+ lines = text.splitlines()
132
+ if len(lines) <= 2 * n:
133
+ return "\n".join(lines)
134
+ return "\n".join(
135
+ lines[:n]
136
+ + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
137
+ + lines[-n:]
138
+ )
139
+
140
+ if stdout_lines <= max_lines and stderr_lines <= max_lines:
141
+ result = f"Return code: {return_code}\n--- python_file_run: STDOUT ---\n{stdout_content}"
142
+ if stderr_content.strip():
143
+ result += f"\n--- python_file_run: STDERR ---\n{stderr_content}"
144
+ return result
145
+ else:
146
+ result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
147
+ if stderr_lines > 0 and stderr_content.strip():
148
+ result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
149
+ result += f"returncode: {return_code}\n"
150
+ result += (
151
+ "--- python_file_run: STDOUT (head/tail) ---\n"
152
+ + head_tail(stdout_content)
153
+ + "\n"
154
+ )
155
+ if stderr_content.strip():
156
+ result += (
157
+ "--- python_file_run: STDERR (head/tail) ---\n"
158
+ + head_tail(stderr_content)
159
+ + "\n"
160
+ )
161
+ result += "Use the view_file tool to inspect the contents of these files when needed."
162
+ return result
@@ -1,14 +1,14 @@
1
- from janito.agent.tool_base import ToolBase
2
- from janito.agent.tools_utils.action_type import ActionType
3
- from janito.agent.tool_registry import register_tool
4
- from janito.agent.tools_utils.utils import pluralize, display_path
1
+ from janito.tools.tool_base import ToolBase
2
+ from janito.report_events import ReportAction
3
+ from janito.tools.adapters.local.adapter import register_local_tool
4
+ from janito.tools.tool_utils import pluralize, display_path
5
5
  from janito.i18n import tr
6
6
  import shutil
7
7
  import os
8
8
  import zipfile
9
9
 
10
10
 
11
- @register_tool(name="remove_directory")
11
+ @register_local_tool
12
12
  class RemoveDirectoryTool(ToolBase):
13
13
  """
14
14
  Remove a directory.
@@ -22,11 +22,13 @@ class RemoveDirectoryTool(ToolBase):
22
22
  - "Error removing directory: <error message>"
23
23
  """
24
24
 
25
+ tool_name = "remove_directory"
26
+
25
27
  def run(self, file_path: str, recursive: bool = False) -> str:
26
28
  disp_path = display_path(file_path)
27
- self.report_info(
28
- ActionType.WRITE,
29
+ self.report_action(
29
30
  tr("🗃️ Remove directory '{disp_path}' ...", disp_path=disp_path),
31
+ ReportAction.CREATE,
30
32
  )
31
33
  backup_zip = None
32
34
  try:
@@ -46,12 +48,16 @@ class RemoveDirectoryTool(ToolBase):
46
48
  else:
47
49
  os.rmdir(file_path)
48
50
  self.report_success(
49
- tr("✅ 1 {dir_word}", dir_word=pluralize("directory", 1))
51
+ tr("✅ 1 {dir_word}", dir_word=pluralize("directory", 1)),
52
+ ReportAction.CREATE,
50
53
  )
51
54
  msg = tr("Directory removed: {disp_path}", disp_path=disp_path)
52
55
  if backup_zip:
53
56
  msg += tr(" (backup at {backup_zip})", backup_zip=backup_zip)
54
57
  return msg
55
58
  except Exception as e:
56
- self.report_error(tr(" ❌ Error removing directory: {error}", error=e))
59
+ self.report_error(
60
+ tr(" ❌ Error removing directory: {error}", error=e),
61
+ ReportAction.REMOVE,
62
+ )
57
63
  return tr("Error removing directory: {error}", error=e)