janito 1.14.3__py3-none-any.whl → 2.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- janito/__init__.py +6 -1
- janito/__main__.py +1 -1
- janito/agent/setup_agent.py +139 -0
- janito/agent/templates/profiles/{system_prompt_template_base.txt.j2 → system_prompt_template_main.txt.j2} +1 -1
- janito/cli/__init__.py +9 -0
- janito/cli/chat_mode/bindings.py +37 -0
- janito/cli/chat_mode/chat_entry.py +23 -0
- janito/cli/chat_mode/prompt_style.py +19 -0
- janito/cli/chat_mode/session.py +272 -0
- janito/{shell/prompt/completer.py → cli/chat_mode/shell/autocomplete.py} +7 -6
- janito/cli/chat_mode/shell/commands/__init__.py +55 -0
- janito/cli/chat_mode/shell/commands/base.py +9 -0
- janito/cli/chat_mode/shell/commands/clear.py +12 -0
- janito/{shell → cli/chat_mode/shell}/commands/conversation_restart.py +34 -30
- janito/cli/chat_mode/shell/commands/edit.py +25 -0
- janito/cli/chat_mode/shell/commands/help.py +16 -0
- janito/cli/chat_mode/shell/commands/history_view.py +93 -0
- janito/cli/chat_mode/shell/commands/lang.py +25 -0
- janito/cli/chat_mode/shell/commands/last.py +137 -0
- janito/cli/chat_mode/shell/commands/livelogs.py +49 -0
- janito/cli/chat_mode/shell/commands/multi.py +51 -0
- janito/cli/chat_mode/shell/commands/prompt.py +64 -0
- janito/cli/chat_mode/shell/commands/role.py +36 -0
- janito/cli/chat_mode/shell/commands/session.py +40 -0
- janito/{shell → cli/chat_mode/shell}/commands/session_control.py +2 -2
- janito/cli/chat_mode/shell/commands/termweb_log.py +92 -0
- janito/cli/chat_mode/shell/commands/tools.py +32 -0
- janito/{shell → cli/chat_mode/shell}/commands/utility.py +4 -7
- janito/{shell → cli/chat_mode/shell}/commands/verbose.py +5 -5
- janito/cli/chat_mode/shell/session/__init__.py +1 -0
- janito/{shell → cli/chat_mode/shell}/session/manager.py +9 -1
- janito/cli/chat_mode/toolbar.py +90 -0
- janito/cli/cli_commands/list_models.py +35 -0
- janito/cli/cli_commands/list_providers.py +9 -0
- janito/cli/cli_commands/list_tools.py +53 -0
- janito/cli/cli_commands/model_selection.py +50 -0
- janito/cli/cli_commands/model_utils.py +84 -0
- janito/cli/cli_commands/set_api_key.py +19 -0
- janito/cli/cli_commands/show_config.py +51 -0
- janito/cli/cli_commands/show_system_prompt.py +62 -0
- janito/cli/config.py +28 -0
- janito/cli/console.py +3 -0
- janito/cli/core/__init__.py +4 -0
- janito/cli/core/event_logger.py +59 -0
- janito/cli/core/getters.py +31 -0
- janito/cli/core/runner.py +141 -0
- janito/cli/core/setters.py +174 -0
- janito/cli/core/unsetters.py +54 -0
- janito/cli/main.py +8 -196
- janito/cli/main_cli.py +312 -0
- janito/cli/prompt_core.py +230 -0
- janito/cli/prompt_handler.py +6 -0
- janito/cli/rich_terminal_reporter.py +101 -0
- janito/cli/single_shot_mode/__init__.py +6 -0
- janito/cli/single_shot_mode/handler.py +137 -0
- janito/cli/termweb_starter.py +73 -24
- janito/cli/utils.py +25 -0
- janito/cli/verbose_output.py +196 -0
- janito/config.py +5 -0
- janito/config_manager.py +110 -0
- janito/conversation_history.py +30 -0
- janito/{agent/tools_utils/dir_walk_utils.py → dir_walk_utils.py} +3 -2
- janito/driver_events.py +98 -0
- janito/drivers/anthropic/driver.py +113 -0
- janito/drivers/azure_openai/driver.py +36 -0
- janito/drivers/driver_registry.py +33 -0
- janito/drivers/google_genai/driver.py +54 -0
- janito/drivers/google_genai/schema_generator.py +67 -0
- janito/drivers/mistralai/driver.py +41 -0
- janito/drivers/openai/driver.py +334 -0
- janito/event_bus/__init__.py +2 -0
- janito/event_bus/bus.py +68 -0
- janito/event_bus/event.py +15 -0
- janito/event_bus/handler.py +31 -0
- janito/event_bus/queue_bus.py +57 -0
- janito/exceptions.py +23 -0
- janito/formatting_token.py +54 -0
- janito/i18n/pt.py +1 -0
- janito/llm/__init__.py +5 -0
- janito/llm/agent.py +443 -0
- janito/llm/auth.py +62 -0
- janito/llm/driver.py +239 -0
- janito/llm/driver_config.py +34 -0
- janito/llm/driver_config_builder.py +34 -0
- janito/llm/driver_input.py +12 -0
- janito/llm/message_parts.py +60 -0
- janito/llm/model.py +38 -0
- janito/llm/provider.py +187 -0
- janito/perf_singleton.py +3 -0
- janito/performance_collector.py +167 -0
- janito/provider_config.py +98 -0
- janito/provider_registry.py +152 -0
- janito/providers/__init__.py +7 -0
- janito/providers/anthropic/model_info.py +22 -0
- janito/providers/anthropic/provider.py +65 -0
- janito/providers/azure_openai/model_info.py +15 -0
- janito/providers/azure_openai/provider.py +72 -0
- janito/providers/deepseek/__init__.py +1 -0
- janito/providers/deepseek/model_info.py +16 -0
- janito/providers/deepseek/provider.py +91 -0
- janito/providers/google/__init__.py +1 -0
- janito/providers/google/model_info.py +40 -0
- janito/providers/google/provider.py +69 -0
- janito/providers/mistralai/model_info.py +37 -0
- janito/providers/mistralai/provider.py +69 -0
- janito/providers/openai/__init__.py +1 -0
- janito/providers/openai/model_info.py +137 -0
- janito/providers/openai/provider.py +107 -0
- janito/providers/openai/schema_generator.py +63 -0
- janito/providers/provider_static_info.py +21 -0
- janito/providers/registry.py +26 -0
- janito/report_events.py +38 -0
- janito/termweb/app.py +1 -1
- janito/tools/__init__.py +16 -0
- janito/tools/adapters/__init__.py +1 -0
- janito/tools/adapters/local/__init__.py +54 -0
- janito/tools/adapters/local/adapter.py +92 -0
- janito/{agent/tools → tools/adapters/local}/ask_user.py +30 -13
- janito/tools/adapters/local/copy_file.py +84 -0
- janito/{agent/tools → tools/adapters/local}/create_directory.py +11 -10
- janito/tools/adapters/local/create_file.py +82 -0
- janito/tools/adapters/local/delete_text_in_file.py +136 -0
- janito/{agent/tools → tools/adapters/local}/fetch_url.py +18 -19
- janito/tools/adapters/local/find_files.py +140 -0
- janito/tools/adapters/local/get_file_outline/core.py +151 -0
- janito/{agent/tools → tools/adapters/local}/get_file_outline/python_outline.py +125 -0
- janito/tools/adapters/local/get_file_outline/python_outline_v2.py +156 -0
- janito/{agent/tools → tools/adapters/local}/get_file_outline/search_outline.py +12 -7
- janito/{agent/tools → tools/adapters/local}/move_file.py +13 -9
- janito/tools/adapters/local/open_html_in_browser.py +34 -0
- janito/{agent/tools → tools/adapters/local}/open_url.py +7 -5
- janito/tools/adapters/local/python_code_run.py +165 -0
- janito/tools/adapters/local/python_command_run.py +163 -0
- janito/tools/adapters/local/python_file_run.py +162 -0
- janito/{agent/tools → tools/adapters/local}/remove_directory.py +15 -9
- janito/{agent/tools → tools/adapters/local}/remove_file.py +17 -14
- janito/{agent/tools → tools/adapters/local}/replace_text_in_file.py +27 -22
- janito/tools/adapters/local/run_bash_command.py +176 -0
- janito/tools/adapters/local/run_powershell_command.py +219 -0
- janito/{agent/tools → tools/adapters/local}/search_text/core.py +32 -12
- janito/{agent/tools → tools/adapters/local}/search_text/match_lines.py +13 -4
- janito/{agent/tools → tools/adapters/local}/search_text/pattern_utils.py +12 -4
- janito/{agent/tools → tools/adapters/local}/search_text/traverse_directory.py +15 -2
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/core.py +12 -11
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/css_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/html_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/js_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/json_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/markdown_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/ps1_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/python_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/xml_validator.py +1 -1
- janito/{agent/tools → tools/adapters/local}/validate_file_syntax/yaml_validator.py +1 -1
- janito/{agent/tools/get_lines.py → tools/adapters/local/view_file.py} +45 -27
- janito/tools/inspect_registry.py +17 -0
- janito/tools/tool_base.py +105 -0
- janito/tools/tool_events.py +58 -0
- janito/tools/tool_run_exception.py +12 -0
- janito/{agent → tools}/tool_use_tracker.py +2 -4
- janito/{agent/tools_utils/utils.py → tools/tool_utils.py} +18 -9
- janito/tools/tools_adapter.py +207 -0
- janito/tools/tools_schema.py +104 -0
- janito/utils.py +11 -0
- janito/version.py +4 -0
- janito-2.0.1.dist-info/METADATA +232 -0
- janito-2.0.1.dist-info/RECORD +181 -0
- janito/agent/__init__.py +0 -0
- janito/agent/api_exceptions.py +0 -4
- janito/agent/config.py +0 -147
- janito/agent/config_defaults.py +0 -12
- janito/agent/config_utils.py +0 -0
- janito/agent/content_handler.py +0 -0
- janito/agent/conversation.py +0 -238
- janito/agent/conversation_api.py +0 -306
- janito/agent/conversation_exceptions.py +0 -18
- janito/agent/conversation_tool_calls.py +0 -39
- janito/agent/conversation_ui.py +0 -17
- janito/agent/event.py +0 -24
- janito/agent/event_dispatcher.py +0 -24
- janito/agent/event_handler_protocol.py +0 -5
- janito/agent/event_system.py +0 -15
- janito/agent/llm_conversation_history.py +0 -82
- janito/agent/message_handler.py +0 -20
- janito/agent/message_handler_protocol.py +0 -5
- janito/agent/openai_client.py +0 -149
- janito/agent/openai_schema_generator.py +0 -187
- janito/agent/profile_manager.py +0 -96
- janito/agent/queued_message_handler.py +0 -50
- janito/agent/rich_live.py +0 -32
- janito/agent/rich_message_handler.py +0 -115
- janito/agent/runtime_config.py +0 -36
- janito/agent/test_handler_protocols.py +0 -47
- janito/agent/test_openai_schema_generator.py +0 -93
- janito/agent/tests/__init__.py +0 -1
- janito/agent/tool_base.py +0 -63
- janito/agent/tool_executor.py +0 -122
- janito/agent/tool_registry.py +0 -49
- janito/agent/tools/__init__.py +0 -47
- janito/agent/tools/create_file.py +0 -59
- janito/agent/tools/delete_text_in_file.py +0 -97
- janito/agent/tools/find_files.py +0 -106
- janito/agent/tools/get_file_outline/core.py +0 -81
- janito/agent/tools/present_choices.py +0 -64
- janito/agent/tools/python_command_runner.py +0 -201
- janito/agent/tools/python_file_runner.py +0 -199
- janito/agent/tools/python_stdin_runner.py +0 -208
- janito/agent/tools/replace_file.py +0 -72
- janito/agent/tools/run_bash_command.py +0 -218
- janito/agent/tools/run_powershell_command.py +0 -251
- janito/agent/tools_utils/__init__.py +0 -1
- janito/agent/tools_utils/action_type.py +0 -7
- janito/agent/tools_utils/test_gitignore_utils.py +0 -46
- janito/cli/_livereload_log_utils.py +0 -13
- janito/cli/_print_config.py +0 -96
- janito/cli/_termweb_log_utils.py +0 -17
- janito/cli/_utils.py +0 -9
- janito/cli/arg_parser.py +0 -272
- janito/cli/cli_main.py +0 -281
- janito/cli/config_commands.py +0 -211
- janito/cli/config_runner.py +0 -35
- janito/cli/formatting_runner.py +0 -12
- janito/cli/livereload_starter.py +0 -60
- janito/cli/logging_setup.py +0 -38
- janito/cli/one_shot.py +0 -80
- janito/livereload/app.py +0 -25
- janito/rich_utils.py +0 -59
- janito/shell/__init__.py +0 -0
- janito/shell/commands/__init__.py +0 -61
- janito/shell/commands/config.py +0 -22
- janito/shell/commands/edit.py +0 -24
- janito/shell/commands/history_view.py +0 -18
- janito/shell/commands/lang.py +0 -19
- janito/shell/commands/livelogs.py +0 -42
- janito/shell/commands/prompt.py +0 -62
- janito/shell/commands/termweb_log.py +0 -94
- janito/shell/commands/tools.py +0 -26
- janito/shell/commands/track.py +0 -36
- janito/shell/main.py +0 -326
- janito/shell/prompt/load_prompt.py +0 -57
- janito/shell/prompt/session_setup.py +0 -57
- janito/shell/session/config.py +0 -109
- janito/shell/session/history.py +0 -0
- janito/shell/ui/interactive.py +0 -226
- janito/termweb/static/editor.css +0 -158
- janito/termweb/static/editor.css.bak +0 -145
- janito/termweb/static/editor.html +0 -46
- janito/termweb/static/editor.html.bak +0 -46
- janito/termweb/static/editor.js +0 -265
- janito/termweb/static/editor.js.bak +0 -259
- janito/termweb/static/explorer.html.bak +0 -59
- janito/termweb/static/favicon.ico +0 -0
- janito/termweb/static/favicon.ico.bak +0 -0
- janito/termweb/static/index.html +0 -53
- janito/termweb/static/index.html.bak +0 -54
- janito/termweb/static/index.html.bak.bak +0 -175
- janito/termweb/static/landing.html.bak +0 -36
- janito/termweb/static/termicon.svg +0 -1
- janito/termweb/static/termweb.css +0 -214
- janito/termweb/static/termweb.css.bak +0 -237
- janito/termweb/static/termweb.js +0 -162
- janito/termweb/static/termweb.js.bak +0 -168
- janito/termweb/static/termweb.js.bak.bak +0 -157
- janito/termweb/static/termweb_quickopen.js +0 -135
- janito/termweb/static/termweb_quickopen.js.bak +0 -125
- janito/tests/test_rich_utils.py +0 -44
- janito/web/__init__.py +0 -0
- janito/web/__main__.py +0 -25
- janito/web/app.py +0 -145
- janito-1.14.3.dist-info/METADATA +0 -313
- janito-1.14.3.dist-info/RECORD +0 -162
- janito-1.14.3.dist-info/licenses/LICENSE +0 -21
- /janito/{shell → cli/chat_mode/shell}/input_history.py +0 -0
- /janito/{shell/commands/session.py → cli/chat_mode/shell/session/history.py} +0 -0
- /janito/{agent/tools_utils/formatting.py → formatting.py} +0 -0
- /janito/{agent/tools_utils/gitignore_utils.py → gitignore_utils.py} +0 -0
- /janito/{agent/platform_discovery.py → platform_discovery.py} +0 -0
- /janito/{agent/tools → tools/adapters/local}/get_file_outline/__init__.py +0 -0
- /janito/{agent/tools → tools/adapters/local}/get_file_outline/markdown_outline.py +0 -0
- /janito/{agent/tools → tools/adapters/local}/search_text/__init__.py +0 -0
- /janito/{agent/tools → tools/adapters/local}/validate_file_syntax/__init__.py +0 -0
- {janito-1.14.3.dist-info → janito-2.0.1.dist-info}/WHEEL +0 -0
- {janito-1.14.3.dist-info → janito-2.0.1.dist-info}/entry_points.txt +0 -0
- {janito-1.14.3.dist-info → janito-2.0.1.dist-info}/top_level.txt +0 -0
@@ -1,24 +1,29 @@
|
|
1
|
-
from janito.
|
2
|
-
from janito.
|
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.
|
18
|
+
from janito.tools.tool_utils import display_path
|
14
19
|
from janito.i18n import tr
|
15
20
|
|
16
|
-
self.
|
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.
|
4
|
-
from janito.
|
5
|
-
from janito.
|
6
|
-
from janito.
|
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
|
-
@
|
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.
|
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
|
@@ -0,0 +1,34 @@
|
|
1
|
+
from janito.tools.tool_base import ToolBase
|
2
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
3
|
+
import webbrowser
|
4
|
+
import os
|
5
|
+
|
6
|
+
@register_local_tool
|
7
|
+
class OpenHtmlInBrowserTool(ToolBase):
|
8
|
+
"""
|
9
|
+
Opens an HTML file in the default web browser.
|
10
|
+
|
11
|
+
Args:
|
12
|
+
file_path (str): The path to the HTML file to open.
|
13
|
+
|
14
|
+
Returns:
|
15
|
+
str: Status message indicating the result. Example:
|
16
|
+
- "✅ Successfully opened the file in the default browser."
|
17
|
+
- "⚠️ Error: The specified file does not exist."
|
18
|
+
- "⚠️ Error: The specified file is not an HTML file."
|
19
|
+
"""
|
20
|
+
|
21
|
+
tool_name = "open_html_in_browser"
|
22
|
+
|
23
|
+
def run(self, file_path: str) -> str:
|
24
|
+
if not os.path.exists(file_path):
|
25
|
+
return "⚠️ Error: The specified file does not exist."
|
26
|
+
|
27
|
+
if not file_path.lower().endswith('.html'):
|
28
|
+
return "⚠️ Error: The specified file is not an HTML file."
|
29
|
+
|
30
|
+
try:
|
31
|
+
webbrowser.open(f"file://{os.path.abspath(file_path)}")
|
32
|
+
return "✅ Opened."
|
33
|
+
except Exception as e:
|
34
|
+
return f"⚠️ Error: {str(e)}"
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import webbrowser
|
2
|
-
from janito.
|
3
|
-
from janito.
|
4
|
-
from janito.
|
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
|
-
@
|
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.
|
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
|