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,199 +0,0 @@
|
|
1
|
-
import subprocess
|
2
|
-
import os
|
3
|
-
import sys
|
4
|
-
import tempfile
|
5
|
-
import threading
|
6
|
-
from janito.agent.tool_base import ToolBase
|
7
|
-
from janito.agent.tools_utils.action_type import ActionType
|
8
|
-
from janito.agent.tool_registry import register_tool
|
9
|
-
from janito.i18n import tr
|
10
|
-
from janito.agent.runtime_config import runtime_config
|
11
|
-
|
12
|
-
|
13
|
-
@register_tool(name="python_file_runner")
|
14
|
-
class PythonFileRunnerTool(ToolBase):
|
15
|
-
"""
|
16
|
-
Tool to execute a specified Python script file.
|
17
|
-
Args:
|
18
|
-
file_path (str): Path to the Python script file to execute.
|
19
|
-
timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
|
20
|
-
Returns:
|
21
|
-
str: Output and status message, or file paths/line counts if output is large.
|
22
|
-
"""
|
23
|
-
|
24
|
-
def run(self, file_path: str, timeout: int = 60) -> str:
|
25
|
-
self.report_info(
|
26
|
-
ActionType.EXECUTE,
|
27
|
-
tr("680 Running: python {file_path}", file_path=file_path),
|
28
|
-
)
|
29
|
-
try:
|
30
|
-
if runtime_config.get("all_out"):
|
31
|
-
process = subprocess.Popen(
|
32
|
-
[sys.executable, file_path],
|
33
|
-
stdout=subprocess.PIPE,
|
34
|
-
stderr=subprocess.PIPE,
|
35
|
-
text=True,
|
36
|
-
bufsize=1,
|
37
|
-
universal_newlines=True,
|
38
|
-
encoding="utf-8",
|
39
|
-
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
40
|
-
)
|
41
|
-
stdout_accum = []
|
42
|
-
stderr_accum = []
|
43
|
-
|
44
|
-
def read_stream(stream, report_func, accum):
|
45
|
-
for line in stream:
|
46
|
-
accum.append(line)
|
47
|
-
report_func(line)
|
48
|
-
|
49
|
-
stdout_thread = threading.Thread(
|
50
|
-
target=read_stream,
|
51
|
-
args=(process.stdout, self.report_stdout, stdout_accum),
|
52
|
-
)
|
53
|
-
stderr_thread = threading.Thread(
|
54
|
-
target=read_stream,
|
55
|
-
args=(process.stderr, self.report_stderr, stderr_accum),
|
56
|
-
)
|
57
|
-
stdout_thread.start()
|
58
|
-
stderr_thread.start()
|
59
|
-
try:
|
60
|
-
return_code = process.wait(timeout=timeout)
|
61
|
-
except subprocess.TimeoutExpired:
|
62
|
-
process.kill()
|
63
|
-
self.report_error(
|
64
|
-
tr("6d1 Timed out after {timeout} seconds.", timeout=timeout)
|
65
|
-
)
|
66
|
-
return tr(
|
67
|
-
"Code timed out after {timeout} seconds.", timeout=timeout
|
68
|
-
)
|
69
|
-
stdout_thread.join()
|
70
|
-
stderr_thread.join()
|
71
|
-
self.report_success(
|
72
|
-
tr("197 Return code {return_code}", return_code=return_code)
|
73
|
-
)
|
74
|
-
stdout = "".join(stdout_accum)
|
75
|
-
stderr = "".join(stderr_accum)
|
76
|
-
result = f"Return code: {return_code}\n--- STDOUT ---\n{stdout}"
|
77
|
-
if stderr and stderr.strip():
|
78
|
-
result += f"\n--- STDERR ---\n{stderr}"
|
79
|
-
return result
|
80
|
-
else:
|
81
|
-
with (
|
82
|
-
tempfile.NamedTemporaryFile(
|
83
|
-
mode="w+",
|
84
|
-
prefix="python_file_stdout_",
|
85
|
-
delete=False,
|
86
|
-
encoding="utf-8",
|
87
|
-
) as stdout_file,
|
88
|
-
tempfile.NamedTemporaryFile(
|
89
|
-
mode="w+",
|
90
|
-
prefix="python_file_stderr_",
|
91
|
-
delete=False,
|
92
|
-
encoding="utf-8",
|
93
|
-
) as stderr_file,
|
94
|
-
):
|
95
|
-
process = subprocess.Popen(
|
96
|
-
[sys.executable, file_path],
|
97
|
-
stdout=subprocess.PIPE,
|
98
|
-
stderr=subprocess.PIPE,
|
99
|
-
text=True,
|
100
|
-
bufsize=1,
|
101
|
-
universal_newlines=True,
|
102
|
-
encoding="utf-8",
|
103
|
-
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
104
|
-
)
|
105
|
-
stdout_lines, stderr_lines = self._stream_process_output(
|
106
|
-
process, stdout_file, stderr_file
|
107
|
-
)
|
108
|
-
return_code = self._wait_for_process(process, timeout)
|
109
|
-
if return_code is None:
|
110
|
-
return tr(
|
111
|
-
"Code timed out after {timeout} seconds.", timeout=timeout
|
112
|
-
)
|
113
|
-
stdout_file.flush()
|
114
|
-
stderr_file.flush()
|
115
|
-
self.report_success(
|
116
|
-
tr("197 Return code {return_code}", return_code=return_code)
|
117
|
-
)
|
118
|
-
return self._format_result(
|
119
|
-
stdout_file.name, stderr_file.name, return_code
|
120
|
-
)
|
121
|
-
except Exception as e:
|
122
|
-
self.report_error(tr("534 Error: {error}", error=e))
|
123
|
-
return tr("Error running file: {error}", error=e)
|
124
|
-
|
125
|
-
def _stream_process_output(self, process, stdout_file, stderr_file):
|
126
|
-
stdout_lines = 0
|
127
|
-
stderr_lines = 0
|
128
|
-
|
129
|
-
def stream_output(stream, file_obj, report_func, count_func):
|
130
|
-
nonlocal stdout_lines, stderr_lines
|
131
|
-
for line in stream:
|
132
|
-
file_obj.write(line)
|
133
|
-
file_obj.flush()
|
134
|
-
report_func(line)
|
135
|
-
if count_func == "stdout":
|
136
|
-
stdout_lines += 1
|
137
|
-
else:
|
138
|
-
stderr_lines += 1
|
139
|
-
|
140
|
-
stdout_thread = threading.Thread(
|
141
|
-
target=stream_output,
|
142
|
-
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
143
|
-
)
|
144
|
-
stderr_thread = threading.Thread(
|
145
|
-
target=stream_output,
|
146
|
-
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
147
|
-
)
|
148
|
-
stdout_thread.start()
|
149
|
-
stderr_thread.start()
|
150
|
-
stdout_thread.join()
|
151
|
-
stderr_thread.join()
|
152
|
-
return stdout_lines, stderr_lines
|
153
|
-
|
154
|
-
def _wait_for_process(self, process, timeout):
|
155
|
-
try:
|
156
|
-
return process.wait(timeout=timeout)
|
157
|
-
except subprocess.TimeoutExpired:
|
158
|
-
process.kill()
|
159
|
-
self.report_error(
|
160
|
-
tr("6d1 Timed out after {timeout} seconds.", timeout=timeout)
|
161
|
-
)
|
162
|
-
return None
|
163
|
-
|
164
|
-
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
165
|
-
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
166
|
-
stdout_content = out_f.read()
|
167
|
-
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
168
|
-
stderr_content = err_f.read()
|
169
|
-
max_lines = 100
|
170
|
-
stdout_lines = stdout_content.count("\n")
|
171
|
-
stderr_lines = stderr_content.count("\n")
|
172
|
-
|
173
|
-
def head_tail(text, n=10):
|
174
|
-
lines = text.splitlines()
|
175
|
-
if len(lines) <= 2 * n:
|
176
|
-
return "\n".join(lines)
|
177
|
-
return "\n".join(
|
178
|
-
lines[:n]
|
179
|
-
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
180
|
-
+ lines[-n:]
|
181
|
-
)
|
182
|
-
|
183
|
-
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
184
|
-
result = f"Return code: {return_code}\n--- STDOUT ---\n{stdout_content}"
|
185
|
-
if stderr_content.strip():
|
186
|
-
result += f"\n--- STDERR ---\n{stderr_content}"
|
187
|
-
return result
|
188
|
-
else:
|
189
|
-
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
190
|
-
if stderr_lines > 0 and stderr_content.strip():
|
191
|
-
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
192
|
-
result += f"returncode: {return_code}\n"
|
193
|
-
result += "--- STDOUT (head/tail) ---\n" + head_tail(stdout_content) + "\n"
|
194
|
-
if stderr_content.strip():
|
195
|
-
result += (
|
196
|
-
"--- STDERR (head/tail) ---\n" + head_tail(stderr_content) + "\n"
|
197
|
-
)
|
198
|
-
result += "Use the get_lines tool to inspect the contents of these files when needed."
|
199
|
-
return result
|
@@ -1,208 +0,0 @@
|
|
1
|
-
import subprocess
|
2
|
-
import os
|
3
|
-
import sys
|
4
|
-
import tempfile
|
5
|
-
import threading
|
6
|
-
from janito.agent.tool_base import ToolBase
|
7
|
-
from janito.agent.tools_utils.action_type import ActionType
|
8
|
-
from janito.agent.tool_registry import register_tool
|
9
|
-
from janito.i18n import tr
|
10
|
-
from janito.agent.runtime_config import runtime_config
|
11
|
-
|
12
|
-
|
13
|
-
@register_tool(name="python_stdin_runner")
|
14
|
-
class PythonStdinRunnerTool(ToolBase):
|
15
|
-
"""
|
16
|
-
Tool to execute Python code by passing it to the interpreter via standard input (stdin).
|
17
|
-
Args:
|
18
|
-
code (str): The Python code to execute as a string.
|
19
|
-
timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
|
20
|
-
Returns:
|
21
|
-
str: Output and status message, or file paths/line counts if output is large.
|
22
|
-
"""
|
23
|
-
|
24
|
-
def run(self, code: str, timeout: int = 60) -> str:
|
25
|
-
if not code.strip():
|
26
|
-
self.report_warning(tr("ℹ️ Empty code provided."))
|
27
|
-
return tr("Warning: Empty code provided. Operation skipped.")
|
28
|
-
self.report_info(
|
29
|
-
ActionType.EXECUTE,
|
30
|
-
tr("5e1 Running: python (stdin mode) ...\n{code}\n", code=code),
|
31
|
-
)
|
32
|
-
try:
|
33
|
-
if runtime_config.get("all_out"):
|
34
|
-
process = subprocess.Popen(
|
35
|
-
[sys.executable],
|
36
|
-
stdin=subprocess.PIPE,
|
37
|
-
stdout=subprocess.PIPE,
|
38
|
-
stderr=subprocess.PIPE,
|
39
|
-
text=True,
|
40
|
-
bufsize=1,
|
41
|
-
universal_newlines=True,
|
42
|
-
encoding="utf-8",
|
43
|
-
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
44
|
-
)
|
45
|
-
stdout_accum = []
|
46
|
-
stderr_accum = []
|
47
|
-
|
48
|
-
def read_stream(stream, report_func, accum):
|
49
|
-
for line in stream:
|
50
|
-
accum.append(line)
|
51
|
-
report_func(line)
|
52
|
-
|
53
|
-
stdout_thread = threading.Thread(
|
54
|
-
target=read_stream,
|
55
|
-
args=(process.stdout, self.report_stdout, stdout_accum),
|
56
|
-
)
|
57
|
-
stderr_thread = threading.Thread(
|
58
|
-
target=read_stream,
|
59
|
-
args=(process.stderr, self.report_stderr, stderr_accum),
|
60
|
-
)
|
61
|
-
stdout_thread.start()
|
62
|
-
stderr_thread.start()
|
63
|
-
process.stdin.write(code)
|
64
|
-
process.stdin.close()
|
65
|
-
try:
|
66
|
-
return_code = process.wait(timeout=timeout)
|
67
|
-
except subprocess.TimeoutExpired:
|
68
|
-
process.kill()
|
69
|
-
self.report_error(
|
70
|
-
tr("6d1 Timed out after {timeout} seconds.", timeout=timeout)
|
71
|
-
)
|
72
|
-
return tr(
|
73
|
-
"Code timed out after {timeout} seconds.", timeout=timeout
|
74
|
-
)
|
75
|
-
stdout_thread.join()
|
76
|
-
stderr_thread.join()
|
77
|
-
self.report_success(
|
78
|
-
tr("197 Return code {return_code}", return_code=return_code)
|
79
|
-
)
|
80
|
-
stdout = "".join(stdout_accum)
|
81
|
-
stderr = "".join(stderr_accum)
|
82
|
-
result = f"Return code: {return_code}\n--- STDOUT ---\n{stdout}"
|
83
|
-
if stderr and stderr.strip():
|
84
|
-
result += f"\n--- STDERR ---\n{stderr}"
|
85
|
-
return result
|
86
|
-
else:
|
87
|
-
with (
|
88
|
-
tempfile.NamedTemporaryFile(
|
89
|
-
mode="w+",
|
90
|
-
prefix="python_stdin_stdout_",
|
91
|
-
delete=False,
|
92
|
-
encoding="utf-8",
|
93
|
-
) as stdout_file,
|
94
|
-
tempfile.NamedTemporaryFile(
|
95
|
-
mode="w+",
|
96
|
-
prefix="python_stdin_stderr_",
|
97
|
-
delete=False,
|
98
|
-
encoding="utf-8",
|
99
|
-
) as stderr_file,
|
100
|
-
):
|
101
|
-
process = subprocess.Popen(
|
102
|
-
[sys.executable],
|
103
|
-
stdin=subprocess.PIPE,
|
104
|
-
stdout=subprocess.PIPE,
|
105
|
-
stderr=subprocess.PIPE,
|
106
|
-
text=True,
|
107
|
-
bufsize=1,
|
108
|
-
universal_newlines=True,
|
109
|
-
encoding="utf-8",
|
110
|
-
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
111
|
-
)
|
112
|
-
stdout_lines, stderr_lines = self._stream_process_output(
|
113
|
-
process, stdout_file, stderr_file, code
|
114
|
-
)
|
115
|
-
return_code = self._wait_for_process(process, timeout)
|
116
|
-
if return_code is None:
|
117
|
-
return tr(
|
118
|
-
"Code timed out after {timeout} seconds.", timeout=timeout
|
119
|
-
)
|
120
|
-
stdout_file.flush()
|
121
|
-
stderr_file.flush()
|
122
|
-
self.report_success(
|
123
|
-
tr("197 Return code {return_code}", return_code=return_code)
|
124
|
-
)
|
125
|
-
return self._format_result(
|
126
|
-
stdout_file.name, stderr_file.name, return_code
|
127
|
-
)
|
128
|
-
except Exception as e:
|
129
|
-
self.report_error(tr("534 Error: {error}", error=e))
|
130
|
-
return tr("Error running code via stdin: {error}", error=e)
|
131
|
-
|
132
|
-
def _stream_process_output(self, process, stdout_file, stderr_file, code):
|
133
|
-
stdout_lines = 0
|
134
|
-
stderr_lines = 0
|
135
|
-
|
136
|
-
def stream_output(stream, file_obj, report_func, count_func):
|
137
|
-
nonlocal stdout_lines, stderr_lines
|
138
|
-
for line in stream:
|
139
|
-
file_obj.write(line)
|
140
|
-
file_obj.flush()
|
141
|
-
report_func(line)
|
142
|
-
if count_func == "stdout":
|
143
|
-
stdout_lines += 1
|
144
|
-
else:
|
145
|
-
stderr_lines += 1
|
146
|
-
|
147
|
-
stdout_thread = threading.Thread(
|
148
|
-
target=stream_output,
|
149
|
-
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
150
|
-
)
|
151
|
-
stderr_thread = threading.Thread(
|
152
|
-
target=stream_output,
|
153
|
-
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
154
|
-
)
|
155
|
-
stdout_thread.start()
|
156
|
-
stderr_thread.start()
|
157
|
-
process.stdin.write(code)
|
158
|
-
process.stdin.close()
|
159
|
-
stdout_thread.join()
|
160
|
-
stderr_thread.join()
|
161
|
-
return stdout_lines, stderr_lines
|
162
|
-
|
163
|
-
def _wait_for_process(self, process, timeout):
|
164
|
-
try:
|
165
|
-
return process.wait(timeout=timeout)
|
166
|
-
except subprocess.TimeoutExpired:
|
167
|
-
process.kill()
|
168
|
-
self.report_error(
|
169
|
-
tr("6d1 Timed out after {timeout} seconds.", timeout=timeout)
|
170
|
-
)
|
171
|
-
return None
|
172
|
-
|
173
|
-
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
174
|
-
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
175
|
-
stdout_content = out_f.read()
|
176
|
-
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
177
|
-
stderr_content = err_f.read()
|
178
|
-
max_lines = 100
|
179
|
-
stdout_lines = stdout_content.count("\n")
|
180
|
-
stderr_lines = stderr_content.count("\n")
|
181
|
-
|
182
|
-
def head_tail(text, n=10):
|
183
|
-
lines = text.splitlines()
|
184
|
-
if len(lines) <= 2 * n:
|
185
|
-
return "\n".join(lines)
|
186
|
-
return "\n".join(
|
187
|
-
lines[:n]
|
188
|
-
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
189
|
-
+ lines[-n:]
|
190
|
-
)
|
191
|
-
|
192
|
-
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
193
|
-
result = f"Return code: {return_code}\n--- STDOUT ---\n{stdout_content}"
|
194
|
-
if stderr_content.strip():
|
195
|
-
result += f"\n--- STDERR ---\n{stderr_content}"
|
196
|
-
return result
|
197
|
-
else:
|
198
|
-
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
199
|
-
if stderr_lines > 0 and stderr_content.strip():
|
200
|
-
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
201
|
-
result += f"returncode: {return_code}\n"
|
202
|
-
result += "--- STDOUT (head/tail) ---\n" + head_tail(stdout_content) + "\n"
|
203
|
-
if stderr_content.strip():
|
204
|
-
result += (
|
205
|
-
"--- STDERR (head/tail) ---\n" + head_tail(stderr_content) + "\n"
|
206
|
-
)
|
207
|
-
result += "Use the get_lines tool to inspect the contents of these files when needed."
|
208
|
-
return result
|
@@ -1,72 +0,0 @@
|
|
1
|
-
import os
|
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
|
7
|
-
from janito.i18n import tr
|
8
|
-
|
9
|
-
from janito.agent.tools.validate_file_syntax.core import validate_file_syntax
|
10
|
-
|
11
|
-
|
12
|
-
@register_tool(name="replace_file")
|
13
|
-
class ReplaceFileTool(ToolBase):
|
14
|
-
"""
|
15
|
-
Replace the entire content of an existing file. Fails if the file does not exist.
|
16
|
-
Args:
|
17
|
-
file_path (str): Path to the file to replace content.
|
18
|
-
content (str): The full new content to write to the file. You must provide the complete content as it will fully overwrite the existing file—do not use placeholders for original content.
|
19
|
-
Returns:
|
20
|
-
str: Status message indicating the result. Example:
|
21
|
-
- "✅ Successfully replaced the file at ..."
|
22
|
-
|
23
|
-
Note: Syntax validation is automatically performed after this operation.
|
24
|
-
"""
|
25
|
-
|
26
|
-
def run(self, file_path: str, content: str) -> str:
|
27
|
-
from janito.agent.tool_use_tracker import ToolUseTracker
|
28
|
-
|
29
|
-
expanded_file_path = file_path # Using file_path as is
|
30
|
-
disp_path = display_path(expanded_file_path)
|
31
|
-
file_path = expanded_file_path
|
32
|
-
if not os.path.exists(file_path):
|
33
|
-
return tr(
|
34
|
-
"❗ Cannot replace: file does not exist at '{disp_path}'.",
|
35
|
-
disp_path=disp_path,
|
36
|
-
)
|
37
|
-
# Check previous operation
|
38
|
-
tracker = ToolUseTracker()
|
39
|
-
if not tracker.last_operation_is_full_read_or_replace(file_path):
|
40
|
-
self.report_info(
|
41
|
-
ActionType.WRITE,
|
42
|
-
tr("📝 Replace file '{disp_path}' ...", disp_path=disp_path),
|
43
|
-
)
|
44
|
-
self.report_warning(tr("ℹ️ Missing full view."))
|
45
|
-
try:
|
46
|
-
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
|
47
|
-
current_content = f.read()
|
48
|
-
except Exception as e:
|
49
|
-
current_content = f"[Error reading file: {e}]"
|
50
|
-
return (
|
51
|
-
"⚠️ [missing full view] Update was NOT performed. The full content of the file is included below for your review. Repeat the operation if you wish to proceed.\n"
|
52
|
-
f"--- Current content of {disp_path} ---\n"
|
53
|
-
f"{current_content}"
|
54
|
-
)
|
55
|
-
self.report_info(
|
56
|
-
ActionType.WRITE,
|
57
|
-
tr("📝 Replace file '{disp_path}' ...", disp_path=disp_path),
|
58
|
-
)
|
59
|
-
backup_path = file_path + ".bak"
|
60
|
-
shutil.copy2(file_path, backup_path)
|
61
|
-
with open(file_path, "w", encoding="utf-8", errors="replace") as f:
|
62
|
-
f.write(content)
|
63
|
-
new_lines = content.count("\n") + 1 if content else 0
|
64
|
-
self.report_success(tr("✅ {new_lines} lines", new_lines=new_lines))
|
65
|
-
msg = tr(
|
66
|
-
"✅ Replaced file ({new_lines} lines, backup at {backup_path}).",
|
67
|
-
new_lines=new_lines,
|
68
|
-
backup_path=backup_path,
|
69
|
-
)
|
70
|
-
# Perform syntax validation and append result
|
71
|
-
validation_result = validate_file_syntax(file_path)
|
72
|
-
return msg + f"\n{validation_result}"
|