janito 1.6.0__py3-none-any.whl → 1.8.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.
- janito/__init__.py +1 -1
- janito/agent/config.py +3 -3
- janito/agent/config_defaults.py +3 -2
- janito/agent/conversation.py +73 -27
- janito/agent/conversation_api.py +104 -4
- janito/agent/conversation_exceptions.py +6 -0
- janito/agent/conversation_tool_calls.py +17 -3
- janito/agent/event.py +24 -0
- janito/agent/event_dispatcher.py +24 -0
- janito/agent/event_handler_protocol.py +5 -0
- janito/agent/event_system.py +15 -0
- janito/agent/message_handler.py +4 -1
- janito/agent/message_handler_protocol.py +5 -0
- janito/agent/openai_client.py +5 -6
- janito/agent/openai_schema_generator.py +23 -4
- janito/agent/platform_discovery.py +90 -0
- janito/agent/profile_manager.py +34 -110
- janito/agent/queued_message_handler.py +22 -3
- janito/agent/rich_message_handler.py +3 -1
- janito/agent/templates/profiles/system_prompt_template_base.txt.j2 +14 -0
- janito/agent/templates/profiles/system_prompt_template_base_pt.txt.j2 +13 -0
- janito/agent/test_handler_protocols.py +47 -0
- janito/agent/tests/__init__.py +1 -0
- janito/agent/tool_base.py +1 -1
- janito/agent/tool_executor.py +109 -0
- janito/agent/tool_registry.py +3 -75
- janito/agent/tool_use_tracker.py +46 -0
- janito/agent/tools/__init__.py +11 -8
- janito/agent/tools/ask_user.py +26 -12
- janito/agent/tools/create_directory.py +50 -18
- janito/agent/tools/create_file.py +60 -29
- janito/agent/tools/dir_walk_utils.py +16 -0
- janito/agent/tools/fetch_url.py +10 -11
- janito/agent/tools/find_files.py +49 -40
- janito/agent/tools/get_lines.py +60 -25
- janito/agent/tools/memory.py +48 -0
- janito/agent/tools/move_file.py +72 -23
- janito/agent/tools/outline_file/__init__.py +85 -0
- janito/agent/tools/outline_file/formatting.py +20 -0
- janito/agent/tools/outline_file/markdown_outline.py +14 -0
- janito/agent/tools/outline_file/python_outline.py +71 -0
- janito/agent/tools/present_choices.py +62 -0
- janito/agent/tools/present_choices_test.py +18 -0
- janito/agent/tools/remove_directory.py +31 -26
- janito/agent/tools/remove_file.py +31 -13
- janito/agent/tools/replace_text_in_file.py +135 -36
- janito/agent/tools/run_bash_command.py +113 -97
- janito/agent/tools/run_powershell_command.py +169 -0
- janito/agent/tools/run_python_command.py +53 -29
- janito/agent/tools/search_outline.py +17 -0
- janito/agent/tools/search_text.py +208 -0
- janito/agent/tools/tools_utils.py +47 -4
- janito/agent/tools/utils.py +14 -15
- janito/agent/tools/validate_file_syntax.py +163 -0
- janito/cli/_print_config.py +1 -1
- janito/cli/arg_parser.py +36 -4
- janito/cli/config_commands.py +1 -1
- janito/cli/logging_setup.py +7 -2
- janito/cli/main.py +97 -3
- janito/cli/runner/__init__.py +0 -2
- janito/cli/runner/_termweb_log_utils.py +17 -0
- janito/cli/runner/cli_main.py +121 -89
- janito/cli/runner/config.py +6 -4
- janito/cli/termweb_starter.py +73 -0
- janito/cli_chat_shell/chat_loop.py +52 -13
- janito/cli_chat_shell/chat_state.py +1 -1
- janito/cli_chat_shell/chat_ui.py +2 -3
- janito/cli_chat_shell/commands/__init__.py +17 -6
- janito/cli_chat_shell/commands/{history_reset.py → history_start.py} +13 -5
- janito/cli_chat_shell/commands/lang.py +16 -0
- janito/cli_chat_shell/commands/prompt.py +42 -0
- janito/cli_chat_shell/commands/session_control.py +36 -1
- janito/cli_chat_shell/commands/sum.py +49 -0
- janito/cli_chat_shell/commands/termweb_log.py +86 -0
- janito/cli_chat_shell/commands/utility.py +5 -2
- janito/cli_chat_shell/commands/verbose.py +29 -0
- janito/cli_chat_shell/load_prompt.py +47 -8
- janito/cli_chat_shell/session_manager.py +9 -1
- janito/cli_chat_shell/shell_command_completer.py +20 -0
- janito/cli_chat_shell/ui.py +110 -93
- janito/i18n/__init__.py +35 -0
- janito/i18n/messages.py +23 -0
- janito/i18n/pt.py +46 -0
- janito/rich_utils.py +43 -43
- janito/termweb/app.py +95 -0
- janito/termweb/static/editor.html +238 -0
- janito/termweb/static/editor.html.bak +238 -0
- janito/termweb/static/explorer.html.bak +59 -0
- janito/termweb/static/favicon.ico +0 -0
- janito/termweb/static/favicon.ico.bak +0 -0
- janito/termweb/static/index.html +55 -0
- janito/termweb/static/index.html.bak +55 -0
- janito/termweb/static/index.html.bak.bak +175 -0
- janito/termweb/static/landing.html.bak +36 -0
- janito/termweb/static/termicon.svg +1 -0
- janito/termweb/static/termweb.css +235 -0
- janito/termweb/static/termweb.css.bak +286 -0
- janito/termweb/static/termweb.js +187 -0
- janito/termweb/static/termweb.js.bak +187 -0
- janito/termweb/static/termweb.js.bak.bak +157 -0
- janito/termweb/static/termweb_quickopen.js +135 -0
- janito/termweb/static/termweb_quickopen.js.bak +125 -0
- janito/web/app.py +10 -13
- {janito-1.6.0.dist-info → janito-1.8.0.dist-info}/METADATA +73 -32
- janito-1.8.0.dist-info/RECORD +127 -0
- {janito-1.6.0.dist-info → janito-1.8.0.dist-info}/WHEEL +1 -1
- janito/agent/tool_registry_core.py +0 -2
- janito/agent/tools/get_file_outline.py +0 -117
- janito/agent/tools/py_compile_file.py +0 -40
- janito/agent/tools/replace_file.py +0 -51
- janito/agent/tools/search_files.py +0 -71
- janito/cli/runner/scan.py +0 -44
- janito/cli_chat_shell/commands/system.py +0 -73
- janito-1.6.0.dist-info/RECORD +0 -81
- {janito-1.6.0.dist-info → janito-1.8.0.dist-info}/entry_points.txt +0 -0
- {janito-1.6.0.dist-info → janito-1.8.0.dist-info}/licenses/LICENSE +0 -0
- {janito-1.6.0.dist-info → janito-1.8.0.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
from janito.agent.tool_base import ToolBase
|
2
2
|
from janito.agent.tool_registry import register_tool
|
3
|
-
from janito.
|
3
|
+
from janito.i18n import tr
|
4
4
|
|
5
5
|
|
6
6
|
@register_tool(name="replace_text_in_file")
|
@@ -21,7 +21,7 @@ class ReplaceTextInFileTool(ToolBase):
|
|
21
21
|
- "Error replacing text: <error message>"
|
22
22
|
"""
|
23
23
|
|
24
|
-
def
|
24
|
+
def run(
|
25
25
|
self,
|
26
26
|
file_path: str,
|
27
27
|
search_text: str,
|
@@ -32,43 +32,83 @@ class ReplaceTextInFileTool(ToolBase):
|
|
32
32
|
from janito.agent.tools.tools_utils import display_path
|
33
33
|
|
34
34
|
disp_path = display_path(file_path)
|
35
|
-
action = "all
|
36
|
-
# Show only concise info (lengths, not full content)
|
37
|
-
search_preview = (
|
38
|
-
(search_text[:20] + "...") if len(search_text) > 20 else search_text
|
39
|
-
)
|
40
|
-
replace_preview = (
|
41
|
-
(replacement_text[:20] + "...")
|
42
|
-
if len(replacement_text) > 20
|
43
|
-
else replacement_text
|
44
|
-
)
|
35
|
+
action = "(all)" if replace_all else "(unique)"
|
45
36
|
search_lines = len(search_text.splitlines())
|
46
37
|
replace_lines = len(replacement_text.splitlines())
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
38
|
+
if replace_lines == 0:
|
39
|
+
info_msg = tr(
|
40
|
+
"📝 Replacing in {disp_path} del {search_lines} lines {action}",
|
41
|
+
disp_path=disp_path,
|
42
|
+
search_lines=search_lines,
|
43
|
+
action=action,
|
44
|
+
)
|
45
|
+
else:
|
46
|
+
try:
|
47
|
+
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
|
48
|
+
_content = f.read()
|
49
|
+
_new_content = _content.replace(
|
50
|
+
search_text, replacement_text, -1 if replace_all else 1
|
51
|
+
)
|
52
|
+
_total_lines_before = _content.count("\n") + 1
|
53
|
+
_total_lines_after = _new_content.count("\n") + 1
|
54
|
+
_line_delta = _total_lines_after - _total_lines_before
|
55
|
+
except Exception:
|
56
|
+
_line_delta = replace_lines - search_lines
|
57
|
+
if _line_delta > 0:
|
58
|
+
delta_str = f"+{_line_delta} lines"
|
59
|
+
elif _line_delta < 0:
|
60
|
+
delta_str = f"{_line_delta} lines"
|
61
|
+
else:
|
62
|
+
delta_str = "+0"
|
63
|
+
info_msg = tr(
|
64
|
+
"📝 Replacing in {disp_path} {delta_str} {action}",
|
65
|
+
disp_path=disp_path,
|
66
|
+
delta_str=delta_str,
|
67
|
+
action=action,
|
68
|
+
)
|
69
|
+
self.report_info(
|
70
|
+
info_msg + (" ..." if not info_msg.rstrip().endswith("...") else "")
|
71
|
+
)
|
52
72
|
try:
|
53
73
|
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
|
54
74
|
content = f.read()
|
55
75
|
|
76
|
+
def find_match_lines(content, search_text):
|
77
|
+
lines = content.splitlines(keepends=True)
|
78
|
+
joined = "".join(lines)
|
79
|
+
match_lines = []
|
80
|
+
idx = 0
|
81
|
+
while True:
|
82
|
+
idx = joined.find(search_text, idx)
|
83
|
+
if idx == -1:
|
84
|
+
break
|
85
|
+
upto = joined[:idx]
|
86
|
+
line_no = upto.count("\n") + 1
|
87
|
+
match_lines.append(line_no)
|
88
|
+
idx += 1 if not search_text else len(search_text)
|
89
|
+
return match_lines
|
90
|
+
|
91
|
+
match_lines = find_match_lines(content, search_text)
|
56
92
|
if replace_all:
|
57
93
|
replaced_count = content.count(search_text)
|
58
94
|
new_content = content.replace(search_text, replacement_text)
|
59
95
|
else:
|
60
96
|
occurrences = content.count(search_text)
|
61
97
|
if occurrences > 1:
|
62
|
-
self.report_warning("
|
63
|
-
warning_detail =
|
64
|
-
|
98
|
+
self.report_warning(tr("⚠️ Search text is not unique."))
|
99
|
+
warning_detail = tr(
|
100
|
+
"The search text is not unique. Expand your search context with surrounding lines to ensure uniqueness."
|
101
|
+
)
|
102
|
+
return tr(
|
103
|
+
"No changes made. {warning_detail}",
|
104
|
+
warning_detail=warning_detail,
|
105
|
+
)
|
65
106
|
replaced_count = 1 if occurrences == 1 else 0
|
66
107
|
new_content = content.replace(search_text, replacement_text, 1)
|
67
108
|
import shutil
|
68
109
|
|
69
110
|
backup_path = file_path + ".bak"
|
70
111
|
if backup and new_content != content:
|
71
|
-
# Create a .bak backup before writing changes
|
72
112
|
shutil.copy2(file_path, backup_path)
|
73
113
|
if new_content != content:
|
74
114
|
with open(file_path, "w", encoding="utf-8", errors="replace") as f:
|
@@ -78,17 +118,24 @@ class ReplaceTextInFileTool(ToolBase):
|
|
78
118
|
file_changed = False
|
79
119
|
warning = ""
|
80
120
|
if replaced_count == 0:
|
81
|
-
warning = " [Warning: Search text not found in file]"
|
121
|
+
warning = tr(" [Warning: Search text not found in file]")
|
82
122
|
if not file_changed:
|
83
|
-
self.report_warning("
|
84
|
-
concise_warning =
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
123
|
+
self.report_warning(tr(" ℹ️ No changes made."))
|
124
|
+
concise_warning = tr(
|
125
|
+
"The search text was not found. Expand your search context with surrounding lines if needed."
|
126
|
+
)
|
127
|
+
return tr(
|
128
|
+
"No changes made. {concise_warning}",
|
129
|
+
concise_warning=concise_warning,
|
130
|
+
)
|
131
|
+
if match_lines:
|
132
|
+
lines_str = ", ".join(str(line_no) for line_no in match_lines)
|
133
|
+
self.report_success(
|
134
|
+
tr(" ✅ replaced at {lines_str}", lines_str=lines_str)
|
135
|
+
)
|
136
|
+
else:
|
137
|
+
self.report_success(tr(" ✅ replaced (lines unknown)"))
|
90
138
|
|
91
|
-
# Indentation check for agent warning
|
92
139
|
def leading_ws(line):
|
93
140
|
import re
|
94
141
|
|
@@ -107,11 +154,63 @@ class ReplaceTextInFileTool(ToolBase):
|
|
107
154
|
)
|
108
155
|
indent_warning = ""
|
109
156
|
if search_indent != replace_indent:
|
110
|
-
indent_warning =
|
157
|
+
indent_warning = tr(
|
158
|
+
" [Warning: Indentation mismatch between search and replacement text: '{search_indent}' vs '{replace_indent}']",
|
159
|
+
search_indent=search_indent,
|
160
|
+
replace_indent=replace_indent,
|
161
|
+
)
|
162
|
+
total_lines_before = content.count("\n") + 1
|
163
|
+
total_lines_after = new_content.count("\n") + 1
|
164
|
+
line_delta = total_lines_after - total_lines_before
|
165
|
+
line_delta_str = (
|
166
|
+
f" (+{line_delta} lines)"
|
167
|
+
if line_delta > 0
|
168
|
+
else (
|
169
|
+
f" ({line_delta} lines)"
|
170
|
+
if line_delta < 0
|
171
|
+
else " (no net line change)"
|
172
|
+
)
|
173
|
+
)
|
174
|
+
if replaced_count > 0:
|
175
|
+
if replace_all:
|
176
|
+
match_info = tr(
|
177
|
+
"Matches found at lines: {lines}. ",
|
178
|
+
lines=", ".join(str(line) for line in match_lines),
|
179
|
+
)
|
180
|
+
else:
|
181
|
+
match_info = (
|
182
|
+
tr("Match found at line {line}. ", line=match_lines[0])
|
183
|
+
if match_lines
|
184
|
+
else ""
|
185
|
+
)
|
186
|
+
details = tr(
|
187
|
+
"Replaced {replaced_count} occurrence(s) at above line(s): {search_lines} lines replaced with {replace_lines} lines each.{line_delta_str}",
|
188
|
+
replaced_count=replaced_count,
|
189
|
+
search_lines=search_lines,
|
190
|
+
replace_lines=replace_lines,
|
191
|
+
line_delta_str=line_delta_str,
|
192
|
+
)
|
193
|
+
else:
|
194
|
+
match_info = ""
|
195
|
+
details = ""
|
111
196
|
if "warning_detail" in locals():
|
112
|
-
return
|
113
|
-
|
114
|
-
|
197
|
+
return tr(
|
198
|
+
"Text replaced in {file_path}{warning}{indent_warning} (backup at {backup_path})\n{warning_detail}",
|
199
|
+
file_path=file_path,
|
200
|
+
warning=warning,
|
201
|
+
indent_warning=indent_warning,
|
202
|
+
backup_path=backup_path,
|
203
|
+
warning_detail=warning_detail,
|
204
|
+
)
|
205
|
+
return tr(
|
206
|
+
"Text replaced in {file_path}{warning}{indent_warning} (backup at {backup_path}). {match_info}{details}",
|
207
|
+
file_path=file_path,
|
208
|
+
warning=warning,
|
209
|
+
indent_warning=indent_warning,
|
210
|
+
backup_path=backup_path,
|
211
|
+
match_info=match_info,
|
212
|
+
details=details,
|
213
|
+
)
|
115
214
|
except Exception as e:
|
116
|
-
self.report_error("
|
117
|
-
return
|
215
|
+
self.report_error(tr(" ❌ Error"))
|
216
|
+
return tr("Error replacing text: {error}", error=e)
|
@@ -1,70 +1,48 @@
|
|
1
1
|
from janito.agent.tool_base import ToolBase
|
2
2
|
from janito.agent.tool_registry import register_tool
|
3
|
-
|
3
|
+
from janito.i18n import tr
|
4
4
|
import subprocess
|
5
5
|
import tempfile
|
6
6
|
import sys
|
7
|
+
import os
|
7
8
|
|
8
9
|
|
9
10
|
@register_tool(name="run_bash_command")
|
10
11
|
class RunBashCommandTool(ToolBase):
|
11
12
|
"""
|
12
13
|
Execute a non-interactive command using the bash shell and capture live output.
|
13
|
-
|
14
14
|
This tool explicitly invokes the 'bash' shell (not just the system default shell), so it requires bash to be installed and available in the system PATH. On Windows, this will only work if bash is available (e.g., via WSL, Git Bash, or similar).
|
15
|
-
|
16
15
|
Args:
|
17
16
|
command (str): The bash command to execute.
|
18
17
|
timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
|
19
18
|
require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
|
20
19
|
interactive (bool, optional): If True, warns that the command may require user interaction. Defaults to False. Non-interactive commands are preferred for automation and reliability.
|
21
|
-
|
22
20
|
Returns:
|
23
21
|
str: File paths and line counts for stdout and stderr.
|
24
22
|
"""
|
25
23
|
|
26
|
-
def
|
24
|
+
def run(
|
27
25
|
self,
|
28
26
|
command: str,
|
29
27
|
timeout: int = 60,
|
30
28
|
require_confirmation: bool = False,
|
31
29
|
interactive: bool = False,
|
32
30
|
) -> str:
|
33
|
-
"""
|
34
|
-
Execute a bash command and capture live output.
|
35
|
-
|
36
|
-
Args:
|
37
|
-
command (str): The bash command to execute.
|
38
|
-
timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
|
39
|
-
require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
|
40
|
-
interactive (bool, optional): If True, warns that the command may require user interaction. Defaults to False.
|
41
|
-
|
42
|
-
Returns:
|
43
|
-
str: Output and status message.
|
44
|
-
"""
|
45
|
-
"""
|
46
|
-
Execute a bash command and capture live output.
|
47
|
-
|
48
|
-
Args:
|
49
|
-
command (str): The bash command to execute.
|
50
|
-
timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
|
51
|
-
require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
|
52
|
-
interactive (bool, optional): If True, warns that the command may require user interaction. Defaults to False.
|
53
|
-
|
54
|
-
Returns:
|
55
|
-
str: Output and status message.
|
56
|
-
"""
|
57
31
|
if not command.strip():
|
58
|
-
self.report_warning(
|
59
|
-
|
60
|
-
|
32
|
+
self.report_warning(
|
33
|
+
tr("⚠️ Warning: Empty command provided. Operation skipped.")
|
34
|
+
)
|
35
|
+
return tr("Warning: Empty command provided. Operation skipped.")
|
36
|
+
self.report_info(
|
37
|
+
tr("🖥️ Running bash command: {command} ...\n", command=command)
|
38
|
+
)
|
61
39
|
if interactive:
|
62
40
|
self.report_info(
|
63
|
-
|
41
|
+
tr(
|
42
|
+
"⚠️ Warning: This command might be interactive, require user input, and might hang."
|
43
|
+
)
|
64
44
|
)
|
65
|
-
|
66
45
|
sys.stdout.flush()
|
67
|
-
|
68
46
|
try:
|
69
47
|
with (
|
70
48
|
tempfile.NamedTemporaryFile(
|
@@ -74,82 +52,120 @@ class RunBashCommandTool(ToolBase):
|
|
74
52
|
mode="w+", prefix="run_bash_stderr_", delete=False, encoding="utf-8"
|
75
53
|
) as stderr_file,
|
76
54
|
):
|
77
|
-
|
55
|
+
env = os.environ.copy()
|
56
|
+
env["PYTHONIOENCODING"] = "utf-8"
|
57
|
+
env["LC_ALL"] = "C.UTF-8"
|
58
|
+
env["LANG"] = "C.UTF-8"
|
78
59
|
process = subprocess.Popen(
|
79
60
|
["bash", "-c", command],
|
80
|
-
stdout=
|
81
|
-
stderr=
|
61
|
+
stdout=subprocess.PIPE,
|
62
|
+
stderr=subprocess.PIPE,
|
82
63
|
text=True,
|
64
|
+
encoding="utf-8",
|
65
|
+
bufsize=1,
|
66
|
+
env=env,
|
67
|
+
)
|
68
|
+
stdout_lines = 0
|
69
|
+
stderr_lines = 0
|
70
|
+
stdout_content = []
|
71
|
+
stderr_content = []
|
72
|
+
max_lines = 100
|
73
|
+
import threading
|
74
|
+
|
75
|
+
def stream_reader(
|
76
|
+
stream, file_handle, report_func, content_list, line_counter
|
77
|
+
):
|
78
|
+
for line in iter(stream.readline, ""):
|
79
|
+
file_handle.write(line)
|
80
|
+
file_handle.flush()
|
81
|
+
report_func(line)
|
82
|
+
content_list.append(line)
|
83
|
+
line_counter[0] += 1
|
84
|
+
stream.close()
|
85
|
+
|
86
|
+
stdout_counter = [0]
|
87
|
+
stderr_counter = [0]
|
88
|
+
stdout_thread = threading.Thread(
|
89
|
+
target=stream_reader,
|
90
|
+
args=(
|
91
|
+
process.stdout,
|
92
|
+
stdout_file,
|
93
|
+
self.report_stdout,
|
94
|
+
stdout_content,
|
95
|
+
stdout_counter,
|
96
|
+
),
|
83
97
|
)
|
98
|
+
stderr_thread = threading.Thread(
|
99
|
+
target=stream_reader,
|
100
|
+
args=(
|
101
|
+
process.stderr,
|
102
|
+
stderr_file,
|
103
|
+
self.report_stderr,
|
104
|
+
stderr_content,
|
105
|
+
stderr_counter,
|
106
|
+
),
|
107
|
+
)
|
108
|
+
stdout_thread.start()
|
109
|
+
stderr_thread.start()
|
84
110
|
try:
|
85
|
-
|
111
|
+
process.wait(timeout=timeout)
|
86
112
|
except subprocess.TimeoutExpired:
|
87
113
|
process.kill()
|
88
|
-
self.report_error(
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
102
|
-
) as err_f:
|
103
|
-
err_f.seek(0)
|
104
|
-
for line in err_f:
|
105
|
-
self.report_stderr(line)
|
106
|
-
|
107
|
-
# Count lines
|
108
|
-
with open(
|
109
|
-
stdout_file.name, "r", encoding="utf-8", errors="replace"
|
110
|
-
) as out_f:
|
111
|
-
stdout_lines = sum(1 for _ in out_f)
|
112
|
-
with open(
|
113
|
-
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
114
|
-
) as err_f:
|
115
|
-
stderr_lines = sum(1 for _ in err_f)
|
116
|
-
|
117
|
-
self.report_success(f" ✅ return code {return_code}")
|
114
|
+
self.report_error(
|
115
|
+
tr(" ❌ Timed out after {timeout} seconds.", timeout=timeout)
|
116
|
+
)
|
117
|
+
return tr(
|
118
|
+
"Command timed out after {timeout} seconds.", timeout=timeout
|
119
|
+
)
|
120
|
+
stdout_thread.join()
|
121
|
+
stderr_thread.join()
|
122
|
+
stdout_lines = stdout_counter[0]
|
123
|
+
stderr_lines = stderr_counter[0]
|
124
|
+
self.report_success(
|
125
|
+
tr(" ✅ return code {return_code}", return_code=process.returncode)
|
126
|
+
)
|
118
127
|
warning_msg = ""
|
119
128
|
if interactive:
|
120
|
-
warning_msg =
|
121
|
-
|
122
|
-
|
123
|
-
with open(
|
124
|
-
stdout_file.name, "r", encoding="utf-8", errors="replace"
|
125
|
-
) as out_f:
|
126
|
-
stdout_content = out_f.read()
|
127
|
-
with open(
|
128
|
-
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
129
|
-
) as err_f:
|
130
|
-
stderr_content = err_f.read()
|
131
|
-
|
132
|
-
# Thresholds
|
133
|
-
max_lines = 100
|
129
|
+
warning_msg = tr(
|
130
|
+
"⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
|
131
|
+
)
|
134
132
|
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
135
|
-
|
136
|
-
|
137
|
-
|
133
|
+
with open(
|
134
|
+
stdout_file.name, "r", encoding="utf-8", errors="replace"
|
135
|
+
) as out_f:
|
136
|
+
stdout_content_str = out_f.read()
|
137
|
+
with open(
|
138
|
+
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
139
|
+
) as err_f:
|
140
|
+
stderr_content_str = err_f.read()
|
141
|
+
result = warning_msg + tr(
|
142
|
+
"Return code: {return_code}\n--- STDOUT ---\n{stdout_content}",
|
143
|
+
return_code=process.returncode,
|
144
|
+
stdout_content=stdout_content_str,
|
138
145
|
)
|
139
|
-
if
|
140
|
-
result +=
|
146
|
+
if stderr_content_str.strip():
|
147
|
+
result += tr(
|
148
|
+
"\n--- STDERR ---\n{stderr_content}",
|
149
|
+
stderr_content=stderr_content_str,
|
150
|
+
)
|
141
151
|
return result
|
142
152
|
else:
|
143
|
-
result = (
|
144
|
-
|
145
|
-
|
153
|
+
result = warning_msg + tr(
|
154
|
+
"[LARGE OUTPUT]\nstdout_file: {stdout_file} (lines: {stdout_lines})\n",
|
155
|
+
stdout_file=stdout_file.name,
|
156
|
+
stdout_lines=stdout_lines,
|
146
157
|
)
|
147
|
-
if stderr_lines > 0
|
148
|
-
result += (
|
149
|
-
|
158
|
+
if stderr_lines > 0:
|
159
|
+
result += tr(
|
160
|
+
"stderr_file: {stderr_file} (lines: {stderr_lines})\n",
|
161
|
+
stderr_file=stderr_file.name,
|
162
|
+
stderr_lines=stderr_lines,
|
150
163
|
)
|
151
|
-
result +=
|
164
|
+
result += tr(
|
165
|
+
"returncode: {return_code}\nUse the get_lines tool to inspect the contents of these files when needed.",
|
166
|
+
return_code=process.returncode,
|
167
|
+
)
|
152
168
|
return result
|
153
169
|
except Exception as e:
|
154
|
-
self.report_error(
|
155
|
-
return
|
170
|
+
self.report_error(tr(" ❌ Error: {error}", error=e))
|
171
|
+
return tr("Error running command: {error}", error=e)
|
@@ -0,0 +1,169 @@
|
|
1
|
+
from janito.agent.tool_base import ToolBase
|
2
|
+
from janito.agent.tool_registry import register_tool
|
3
|
+
from janito.i18n import tr
|
4
|
+
import subprocess
|
5
|
+
import tempfile
|
6
|
+
|
7
|
+
|
8
|
+
@register_tool(name="run_powershell_command")
|
9
|
+
class RunPowerShellCommandTool(ToolBase):
|
10
|
+
"""
|
11
|
+
Execute a non-interactive command using the PowerShell shell and capture live output.
|
12
|
+
This tool explicitly invokes 'powershell.exe' (on Windows) or 'pwsh' (on other platforms if available).
|
13
|
+
All commands are automatically prepended with UTF-8 output encoding:
|
14
|
+
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
|
15
|
+
For file output, it is recommended to use -Encoding utf8 in your PowerShell commands (e.g., Out-File -Encoding utf8) to ensure correct file encoding.
|
16
|
+
Args:
|
17
|
+
command (str): The PowerShell command to execute. This string is passed directly to PowerShell using the --Command argument (not as a script file).
|
18
|
+
timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
|
19
|
+
require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
|
20
|
+
interactive (bool, optional): If True, warns that the command may require user interaction. Defaults to False. Non-interactive commands are preferred for automation and reliability.
|
21
|
+
Returns:
|
22
|
+
str: Output and status message, or file paths/line counts if output is large.
|
23
|
+
"""
|
24
|
+
|
25
|
+
def run(
|
26
|
+
self,
|
27
|
+
command: str,
|
28
|
+
timeout: int = 60,
|
29
|
+
require_confirmation: bool = False,
|
30
|
+
interactive: bool = False,
|
31
|
+
) -> str:
|
32
|
+
if not command.strip():
|
33
|
+
self.report_warning(
|
34
|
+
tr("⚠️ Warning: Empty command provided. Operation skipped.")
|
35
|
+
)
|
36
|
+
return tr("Warning: Empty command provided. Operation skipped.")
|
37
|
+
encoding_prefix = "$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; "
|
38
|
+
command_with_encoding = encoding_prefix + command
|
39
|
+
self.report_info(
|
40
|
+
tr("🖥️ Running PowerShell command: {command} ...\n", command=command)
|
41
|
+
)
|
42
|
+
if interactive:
|
43
|
+
self.report_info(
|
44
|
+
tr(
|
45
|
+
"⚠️ Warning: This command might be interactive, require user input, and might hang."
|
46
|
+
)
|
47
|
+
)
|
48
|
+
if require_confirmation:
|
49
|
+
confirmed = self.ask_user_confirmation(
|
50
|
+
tr(
|
51
|
+
"About to run PowerShell command: {command}\nContinue?",
|
52
|
+
command=command,
|
53
|
+
)
|
54
|
+
)
|
55
|
+
if not confirmed:
|
56
|
+
self.report_warning(tr("Execution cancelled by user."))
|
57
|
+
return tr("❌ Command execution cancelled by user.")
|
58
|
+
from janito.agent.platform_discovery import is_windows
|
59
|
+
|
60
|
+
shell_exe = "powershell.exe" if is_windows() else "pwsh"
|
61
|
+
try:
|
62
|
+
with (
|
63
|
+
tempfile.NamedTemporaryFile(
|
64
|
+
mode="w+",
|
65
|
+
prefix="run_powershell_stdout_",
|
66
|
+
delete=False,
|
67
|
+
encoding="utf-8",
|
68
|
+
) as stdout_file,
|
69
|
+
tempfile.NamedTemporaryFile(
|
70
|
+
mode="w+",
|
71
|
+
prefix="run_powershell_stderr_",
|
72
|
+
delete=False,
|
73
|
+
encoding="utf-8",
|
74
|
+
) as stderr_file,
|
75
|
+
):
|
76
|
+
process = subprocess.Popen(
|
77
|
+
[
|
78
|
+
shell_exe,
|
79
|
+
"-NoProfile",
|
80
|
+
"-ExecutionPolicy",
|
81
|
+
"Bypass",
|
82
|
+
"-Command",
|
83
|
+
command_with_encoding,
|
84
|
+
],
|
85
|
+
stdout=stdout_file,
|
86
|
+
stderr=stderr_file,
|
87
|
+
text=True,
|
88
|
+
)
|
89
|
+
try:
|
90
|
+
return_code = process.wait(timeout=timeout)
|
91
|
+
except subprocess.TimeoutExpired:
|
92
|
+
process.kill()
|
93
|
+
self.report_error(
|
94
|
+
tr(" ❌ Timed out after {timeout} seconds.", timeout=timeout)
|
95
|
+
)
|
96
|
+
return tr(
|
97
|
+
"Command timed out after {timeout} seconds.", timeout=timeout
|
98
|
+
)
|
99
|
+
stdout_file.flush()
|
100
|
+
stderr_file.flush()
|
101
|
+
with open(
|
102
|
+
stdout_file.name, "r", encoding="utf-8", errors="replace"
|
103
|
+
) as out_f:
|
104
|
+
out_f.seek(0)
|
105
|
+
for line in out_f:
|
106
|
+
self.report_stdout(line)
|
107
|
+
with open(
|
108
|
+
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
109
|
+
) as err_f:
|
110
|
+
err_f.seek(0)
|
111
|
+
for line in err_f:
|
112
|
+
self.report_stderr(line)
|
113
|
+
with open(
|
114
|
+
stdout_file.name, "r", encoding="utf-8", errors="replace"
|
115
|
+
) as out_f:
|
116
|
+
stdout_lines = sum(1 for _ in out_f)
|
117
|
+
with open(
|
118
|
+
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
119
|
+
) as err_f:
|
120
|
+
stderr_lines = sum(1 for _ in err_f)
|
121
|
+
self.report_success(
|
122
|
+
tr(" ✅ return code {return_code}", return_code=return_code)
|
123
|
+
)
|
124
|
+
warning_msg = ""
|
125
|
+
if interactive:
|
126
|
+
warning_msg = tr(
|
127
|
+
"⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
|
128
|
+
)
|
129
|
+
with open(
|
130
|
+
stdout_file.name, "r", encoding="utf-8", errors="replace"
|
131
|
+
) as out_f:
|
132
|
+
stdout_content = out_f.read()
|
133
|
+
with open(
|
134
|
+
stderr_file.name, "r", encoding="utf-8", errors="replace"
|
135
|
+
) as err_f:
|
136
|
+
stderr_content = err_f.read()
|
137
|
+
max_lines = 100
|
138
|
+
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
139
|
+
result = warning_msg + tr(
|
140
|
+
"Return code: {return_code}\n--- STDOUT ---\n{stdout_content}",
|
141
|
+
return_code=return_code,
|
142
|
+
stdout_content=stdout_content,
|
143
|
+
)
|
144
|
+
if stderr_content.strip():
|
145
|
+
result += tr(
|
146
|
+
"\n--- STDERR ---\n{stderr_content}",
|
147
|
+
stderr_content=stderr_content,
|
148
|
+
)
|
149
|
+
return result
|
150
|
+
else:
|
151
|
+
result = warning_msg + tr(
|
152
|
+
"stdout_file: {stdout_file} (lines: {stdout_lines})\n",
|
153
|
+
stdout_file=stdout_file.name,
|
154
|
+
stdout_lines=stdout_lines,
|
155
|
+
)
|
156
|
+
if stderr_lines > 0 and stderr_content.strip():
|
157
|
+
result += tr(
|
158
|
+
"stderr_file: {stderr_file} (lines: {stderr_lines})\n",
|
159
|
+
stderr_file=stderr_file.name,
|
160
|
+
stderr_lines=stderr_lines,
|
161
|
+
)
|
162
|
+
result += tr(
|
163
|
+
"returncode: {return_code}\nUse the get_lines tool to inspect the contents of these files when needed.",
|
164
|
+
return_code=return_code,
|
165
|
+
)
|
166
|
+
return result
|
167
|
+
except Exception as e:
|
168
|
+
self.report_error(tr(" ❌ Error: {error}", error=e))
|
169
|
+
return tr("Error running command: {error}", error=e)
|