janito 1.7.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.
Files changed (115) hide show
  1. janito/__init__.py +1 -1
  2. janito/agent/config.py +1 -1
  3. janito/agent/config_defaults.py +2 -2
  4. janito/agent/conversation.py +70 -27
  5. janito/agent/conversation_api.py +104 -4
  6. janito/agent/conversation_exceptions.py +6 -0
  7. janito/agent/conversation_tool_calls.py +17 -3
  8. janito/agent/event.py +24 -0
  9. janito/agent/event_dispatcher.py +24 -0
  10. janito/agent/event_handler_protocol.py +5 -0
  11. janito/agent/event_system.py +15 -0
  12. janito/agent/message_handler.py +4 -1
  13. janito/agent/message_handler_protocol.py +5 -0
  14. janito/agent/openai_client.py +5 -8
  15. janito/agent/openai_schema_generator.py +23 -4
  16. janito/agent/profile_manager.py +15 -83
  17. janito/agent/queued_message_handler.py +22 -3
  18. janito/agent/rich_message_handler.py +66 -72
  19. janito/agent/templates/profiles/system_prompt_template_base.txt.j2 +14 -0
  20. janito/agent/templates/profiles/system_prompt_template_base_pt.txt.j2 +13 -0
  21. janito/agent/test_handler_protocols.py +47 -0
  22. janito/agent/tests/__init__.py +1 -0
  23. janito/agent/tool_base.py +1 -1
  24. janito/agent/tool_executor.py +109 -0
  25. janito/agent/tool_registry.py +3 -75
  26. janito/agent/tool_use_tracker.py +46 -0
  27. janito/agent/tools/__init__.py +8 -9
  28. janito/agent/tools/ask_user.py +19 -11
  29. janito/agent/tools/create_directory.py +43 -28
  30. janito/agent/tools/create_file.py +60 -29
  31. janito/agent/tools/dir_walk_utils.py +16 -0
  32. janito/agent/tools/fetch_url.py +10 -11
  33. janito/agent/tools/find_files.py +49 -32
  34. janito/agent/tools/get_lines.py +54 -18
  35. janito/agent/tools/memory.py +32 -52
  36. janito/agent/tools/move_file.py +72 -23
  37. janito/agent/tools/outline_file/__init__.py +85 -0
  38. janito/agent/tools/outline_file/formatting.py +20 -0
  39. janito/agent/tools/outline_file/markdown_outline.py +14 -0
  40. janito/agent/tools/outline_file/python_outline.py +71 -0
  41. janito/agent/tools/present_choices.py +62 -0
  42. janito/agent/tools/present_choices_test.py +18 -0
  43. janito/agent/tools/remove_directory.py +31 -26
  44. janito/agent/tools/remove_file.py +31 -13
  45. janito/agent/tools/replace_text_in_file.py +135 -36
  46. janito/agent/tools/run_bash_command.py +47 -50
  47. janito/agent/tools/run_powershell_command.py +52 -36
  48. janito/agent/tools/run_python_command.py +49 -29
  49. janito/agent/tools/search_outline.py +17 -0
  50. janito/agent/tools/search_text.py +208 -0
  51. janito/agent/tools/tools_utils.py +47 -4
  52. janito/agent/tools/utils.py +14 -15
  53. janito/agent/tools/validate_file_syntax.py +163 -0
  54. janito/cli/arg_parser.py +36 -4
  55. janito/cli/logging_setup.py +7 -2
  56. janito/cli/main.py +96 -2
  57. janito/cli/runner/_termweb_log_utils.py +17 -0
  58. janito/cli/runner/cli_main.py +119 -77
  59. janito/cli/runner/config.py +2 -2
  60. janito/cli/termweb_starter.py +73 -0
  61. janito/cli_chat_shell/chat_loop.py +42 -7
  62. janito/cli_chat_shell/chat_state.py +1 -1
  63. janito/cli_chat_shell/chat_ui.py +0 -1
  64. janito/cli_chat_shell/commands/__init__.py +15 -6
  65. janito/cli_chat_shell/commands/{history_reset.py → history_start.py} +13 -5
  66. janito/cli_chat_shell/commands/lang.py +16 -0
  67. janito/cli_chat_shell/commands/prompt.py +42 -0
  68. janito/cli_chat_shell/commands/session_control.py +36 -1
  69. janito/cli_chat_shell/commands/termweb_log.py +86 -0
  70. janito/cli_chat_shell/commands/utility.py +5 -2
  71. janito/cli_chat_shell/commands/verbose.py +29 -0
  72. janito/cli_chat_shell/session_manager.py +9 -1
  73. janito/cli_chat_shell/shell_command_completer.py +20 -0
  74. janito/cli_chat_shell/ui.py +110 -99
  75. janito/i18n/__init__.py +35 -0
  76. janito/i18n/messages.py +23 -0
  77. janito/i18n/pt.py +46 -0
  78. janito/rich_utils.py +43 -43
  79. janito/termweb/app.py +95 -0
  80. janito/termweb/static/editor.html +238 -0
  81. janito/termweb/static/editor.html.bak +238 -0
  82. janito/termweb/static/explorer.html.bak +59 -0
  83. janito/termweb/static/favicon.ico +0 -0
  84. janito/termweb/static/favicon.ico.bak +0 -0
  85. janito/termweb/static/index.html +55 -0
  86. janito/termweb/static/index.html.bak +55 -0
  87. janito/termweb/static/index.html.bak.bak +175 -0
  88. janito/termweb/static/landing.html.bak +36 -0
  89. janito/termweb/static/termicon.svg +1 -0
  90. janito/termweb/static/termweb.css +235 -0
  91. janito/termweb/static/termweb.css.bak +286 -0
  92. janito/termweb/static/termweb.js +187 -0
  93. janito/termweb/static/termweb.js.bak +187 -0
  94. janito/termweb/static/termweb.js.bak.bak +157 -0
  95. janito/termweb/static/termweb_quickopen.js +135 -0
  96. janito/termweb/static/termweb_quickopen.js.bak +125 -0
  97. janito/web/app.py +4 -4
  98. {janito-1.7.0.dist-info → janito-1.8.0.dist-info}/METADATA +58 -25
  99. janito-1.8.0.dist-info/RECORD +127 -0
  100. {janito-1.7.0.dist-info → janito-1.8.0.dist-info}/WHEEL +1 -1
  101. janito/agent/templates/profiles/system_prompt_template_base.toml +0 -76
  102. janito/agent/templates/profiles/system_prompt_template_default.toml +0 -3
  103. janito/agent/templates/profiles/system_prompt_template_technical.toml +0 -13
  104. janito/agent/tests/test_prompt_toml.py +0 -61
  105. janito/agent/tool_registry_core.py +0 -2
  106. janito/agent/tools/get_file_outline.py +0 -146
  107. janito/agent/tools/py_compile_file.py +0 -40
  108. janito/agent/tools/replace_file.py +0 -51
  109. janito/agent/tools/search_files.py +0 -65
  110. janito/cli/runner/scan.py +0 -57
  111. janito/cli_chat_shell/commands/system.py +0 -73
  112. janito-1.7.0.dist-info/RECORD +0 -89
  113. {janito-1.7.0.dist-info → janito-1.8.0.dist-info}/entry_points.txt +0 -0
  114. {janito-1.7.0.dist-info → janito-1.8.0.dist-info}/licenses/LICENSE +0 -0
  115. {janito-1.7.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.agent.tools.tools_utils import pluralize
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 call(
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 occurrences" if replace_all else None
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
- info_msg = f"\U0001f4dd Replacing in {disp_path}: {search_lines}\u2192{replace_lines} lines"
48
- if action:
49
- info_msg += f" ({action})"
50
- self.report_info(info_msg)
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("\u26a0\ufe0f Search text is not unique.")
63
- warning_detail = "The search text is not unique. Expand your search context with surrounding lines to ensure uniqueness."
64
- return f"No changes made. {warning_detail}"
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(" \u2139\ufe0f No changes made.")
84
- concise_warning = "The search text was not found. Expand your search context with surrounding lines if needed."
85
- return f"No changes made. {concise_warning}"
86
-
87
- self.report_success(
88
- f" \u2705 {replaced_count} {pluralize('block', replaced_count)} replaced"
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 = f" [Warning: Indentation mismatch between search and replacement text: '{search_indent}' vs '{replace_indent}']"
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 f"Text replaced in {file_path}{warning}{indent_warning} (backup at {backup_path})\n{warning_detail}"
113
- return f"Text replaced in {file_path}{warning}{indent_warning} (backup at {backup_path})"
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(" \u274c Error")
117
- return f"Error replacing text: {e}"
215
+ self.report_error(tr(" Error"))
216
+ return tr("Error replacing text: {error}", error=e)
@@ -1,6 +1,6 @@
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
@@ -11,48 +11,38 @@ import os
11
11
  class RunBashCommandTool(ToolBase):
12
12
  """
13
13
  Execute a non-interactive command using the bash shell and capture live output.
14
-
15
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).
16
-
17
15
  Args:
18
16
  command (str): The bash command to execute.
19
17
  timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
20
18
  require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
21
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.
22
-
23
20
  Returns:
24
21
  str: File paths and line counts for stdout and stderr.
25
22
  """
26
23
 
27
- def call(
24
+ def run(
28
25
  self,
29
26
  command: str,
30
27
  timeout: int = 60,
31
28
  require_confirmation: bool = False,
32
29
  interactive: bool = False,
33
30
  ) -> str:
34
- """
35
- Execute a bash command and capture live output.
36
-
37
- Args:
38
- command (str): The bash command to execute.
39
- timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
40
- require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
41
- interactive (bool, optional): If True, warns that the command may require user interaction. Defaults to False.
42
-
43
- Returns:
44
- str: Output and status message.
45
- """
46
31
  if not command.strip():
47
- self.report_warning("⚠️ Warning: Empty command provided. Operation skipped.")
48
- return "Warning: Empty command provided. Operation skipped."
49
- self.report_info(f"🖥️ Running bash command: {command}\n")
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
+ )
50
39
  if interactive:
51
40
  self.report_info(
52
- "⚠️ Warning: This command might be interactive, require user input, and might hang."
41
+ tr(
42
+ "⚠️ Warning: This command might be interactive, require user input, and might hang."
43
+ )
53
44
  )
54
45
  sys.stdout.flush()
55
-
56
46
  try:
57
47
  with (
58
48
  tempfile.NamedTemporaryFile(
@@ -66,23 +56,20 @@ class RunBashCommandTool(ToolBase):
66
56
  env["PYTHONIOENCODING"] = "utf-8"
67
57
  env["LC_ALL"] = "C.UTF-8"
68
58
  env["LANG"] = "C.UTF-8"
69
-
70
59
  process = subprocess.Popen(
71
60
  ["bash", "-c", command],
72
61
  stdout=subprocess.PIPE,
73
62
  stderr=subprocess.PIPE,
74
63
  text=True,
75
64
  encoding="utf-8",
76
- bufsize=1, # line-buffered
65
+ bufsize=1,
77
66
  env=env,
78
67
  )
79
-
80
68
  stdout_lines = 0
81
69
  stderr_lines = 0
82
70
  stdout_content = []
83
71
  stderr_content = []
84
72
  max_lines = 100
85
-
86
73
  import threading
87
74
 
88
75
  def stream_reader(
@@ -120,29 +107,29 @@ class RunBashCommandTool(ToolBase):
120
107
  )
121
108
  stdout_thread.start()
122
109
  stderr_thread.start()
123
-
124
110
  try:
125
111
  process.wait(timeout=timeout)
126
112
  except subprocess.TimeoutExpired:
127
113
  process.kill()
128
- self.report_error(f" ❌ Timed out after {timeout} seconds.")
129
- return f"Command timed out after {timeout} seconds."
130
-
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
+ )
131
120
  stdout_thread.join()
132
121
  stderr_thread.join()
133
-
134
- # Count lines
135
122
  stdout_lines = stdout_counter[0]
136
123
  stderr_lines = stderr_counter[0]
137
-
138
- self.report_success(f" ✅ return code {process.returncode}")
124
+ self.report_success(
125
+ tr(" ✅ return code {return_code}", return_code=process.returncode)
126
+ )
139
127
  warning_msg = ""
140
128
  if interactive:
141
- warning_msg = "⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
142
-
143
- # Read output contents if small
129
+ warning_msg = tr(
130
+ "⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
131
+ )
144
132
  if stdout_lines <= max_lines and stderr_lines <= max_lines:
145
- # Read files from disk to ensure all content is included
146
133
  with open(
147
134
  stdout_file.name, "r", encoding="utf-8", errors="replace"
148
135
  ) as out_f:
@@ -151,24 +138,34 @@ class RunBashCommandTool(ToolBase):
151
138
  stderr_file.name, "r", encoding="utf-8", errors="replace"
152
139
  ) as err_f:
153
140
  stderr_content_str = err_f.read()
154
- result = (
155
- warning_msg
156
- + f"Return code: {process.returncode}\n--- STDOUT ---\n{stdout_content_str}"
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,
157
145
  )
158
146
  if stderr_content_str.strip():
159
- result += f"\n--- STDERR ---\n{stderr_content_str}"
147
+ result += tr(
148
+ "\n--- STDERR ---\n{stderr_content}",
149
+ stderr_content=stderr_content_str,
150
+ )
160
151
  return result
161
152
  else:
162
- result = (
163
- warning_msg
164
- + f"[LARGE OUTPUT]\nstdout_file: {stdout_file.name} (lines: {stdout_lines})\n"
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,
165
157
  )
166
158
  if stderr_lines > 0:
167
- result += (
168
- f"stderr_file: {stderr_file.name} (lines: {stderr_lines})\n"
159
+ result += tr(
160
+ "stderr_file: {stderr_file} (lines: {stderr_lines})\n",
161
+ stderr_file=stderr_file.name,
162
+ stderr_lines=stderr_lines,
169
163
  )
170
- result += f"returncode: {process.returncode}\nUse the get_lines tool to inspect the contents of these files when needed."
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
+ )
171
168
  return result
172
169
  except Exception as e:
173
- self.report_error(f" ❌ Error: {e}")
174
- return f"Error running command: {e}"
170
+ self.report_error(tr(" ❌ Error: {error}", error=e))
171
+ return tr("Error running command: {error}", error=e)
@@ -1,6 +1,6 @@
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
 
@@ -9,25 +9,20 @@ import tempfile
9
9
  class RunPowerShellCommandTool(ToolBase):
10
10
  """
11
11
  Execute a non-interactive command using the PowerShell shell and capture live output.
12
-
13
12
  This tool explicitly invokes 'powershell.exe' (on Windows) or 'pwsh' (on other platforms if available).
14
-
15
13
  All commands are automatically prepended with UTF-8 output encoding:
16
14
  $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
17
-
18
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.
19
-
20
16
  Args:
21
17
  command (str): The PowerShell command to execute. This string is passed directly to PowerShell using the --Command argument (not as a script file).
22
18
  timeout (int, optional): Timeout in seconds for the command. Defaults to 60.
23
19
  require_confirmation (bool, optional): If True, require user confirmation before running. Defaults to False.
24
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.
25
-
26
21
  Returns:
27
22
  str: Output and status message, or file paths/line counts if output is large.
28
23
  """
29
24
 
30
- def call(
25
+ def run(
31
26
  self,
32
27
  command: str,
33
28
  timeout: int = 60,
@@ -35,23 +30,31 @@ class RunPowerShellCommandTool(ToolBase):
35
30
  interactive: bool = False,
36
31
  ) -> str:
37
32
  if not command.strip():
38
- self.report_warning("⚠️ Warning: Empty command provided. Operation skipped.")
39
- return "Warning: Empty command provided. Operation skipped."
40
- # Prepend UTF-8 output encoding
33
+ self.report_warning(
34
+ tr("⚠️ Warning: Empty command provided. Operation skipped.")
35
+ )
36
+ return tr("Warning: Empty command provided. Operation skipped.")
41
37
  encoding_prefix = "$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; "
42
38
  command_with_encoding = encoding_prefix + command
43
- self.report_info(f"🖥️ Running PowerShell command: {command}\n")
39
+ self.report_info(
40
+ tr("🖥️ Running PowerShell command: {command} ...\n", command=command)
41
+ )
44
42
  if interactive:
45
43
  self.report_info(
46
- "⚠️ Warning: This command might be interactive, require user input, and might hang."
44
+ tr(
45
+ "⚠️ Warning: This command might be interactive, require user input, and might hang."
46
+ )
47
47
  )
48
48
  if require_confirmation:
49
49
  confirmed = self.ask_user_confirmation(
50
- f"About to run PowerShell command: {command}\nContinue?"
50
+ tr(
51
+ "About to run PowerShell command: {command}\nContinue?",
52
+ command=command,
53
+ )
51
54
  )
52
55
  if not confirmed:
53
- self.report_warning("Execution cancelled by user.")
54
- return "❌ Command execution cancelled by user."
56
+ self.report_warning(tr("Execution cancelled by user."))
57
+ return tr("❌ Command execution cancelled by user.")
55
58
  from janito.agent.platform_discovery import is_windows
56
59
 
57
60
  shell_exe = "powershell.exe" if is_windows() else "pwsh"
@@ -87,9 +90,12 @@ class RunPowerShellCommandTool(ToolBase):
87
90
  return_code = process.wait(timeout=timeout)
88
91
  except subprocess.TimeoutExpired:
89
92
  process.kill()
90
- self.report_error(f" ❌ Timed out after {timeout} seconds.")
91
- return f"Command timed out after {timeout} seconds."
92
- # Print live output to user
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
+ )
93
99
  stdout_file.flush()
94
100
  stderr_file.flush()
95
101
  with open(
@@ -104,7 +110,6 @@ class RunPowerShellCommandTool(ToolBase):
104
110
  err_f.seek(0)
105
111
  for line in err_f:
106
112
  self.report_stderr(line)
107
- # Count lines
108
113
  with open(
109
114
  stdout_file.name, "r", encoding="utf-8", errors="replace"
110
115
  ) as out_f:
@@ -113,11 +118,14 @@ class RunPowerShellCommandTool(ToolBase):
113
118
  stderr_file.name, "r", encoding="utf-8", errors="replace"
114
119
  ) as err_f:
115
120
  stderr_lines = sum(1 for _ in err_f)
116
- self.report_success(f" ✅ return code {return_code}")
121
+ self.report_success(
122
+ tr(" ✅ return code {return_code}", return_code=return_code)
123
+ )
117
124
  warning_msg = ""
118
125
  if interactive:
119
- warning_msg = "⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
120
- # Read output contents
126
+ warning_msg = tr(
127
+ "⚠️ Warning: This command might be interactive, require user input, and might hang.\n"
128
+ )
121
129
  with open(
122
130
  stdout_file.name, "r", encoding="utf-8", errors="replace"
123
131
  ) as out_f:
@@ -126,28 +134,36 @@ class RunPowerShellCommandTool(ToolBase):
126
134
  stderr_file.name, "r", encoding="utf-8", errors="replace"
127
135
  ) as err_f:
128
136
  stderr_content = err_f.read()
129
- # Thresholds
130
137
  max_lines = 100
131
138
  if stdout_lines <= max_lines and stderr_lines <= max_lines:
132
- result = (
133
- warning_msg
134
- + f"Return code: {return_code}\n--- STDOUT ---\n{stdout_content}"
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,
135
143
  )
136
144
  if stderr_content.strip():
137
- result += f"\n--- STDERR ---\n{stderr_content}"
145
+ result += tr(
146
+ "\n--- STDERR ---\n{stderr_content}",
147
+ stderr_content=stderr_content,
148
+ )
138
149
  return result
139
150
  else:
140
- result = (
141
- warning_msg
142
- + f"stdout_file: {stdout_file.name} (lines: {stdout_lines})\n"
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,
143
155
  )
144
156
  if stderr_lines > 0 and stderr_content.strip():
145
- result += (
146
- f"stderr_file: {stderr_file.name} (lines: {stderr_lines})\n"
157
+ result += tr(
158
+ "stderr_file: {stderr_file} (lines: {stderr_lines})\n",
159
+ stderr_file=stderr_file.name,
160
+ stderr_lines=stderr_lines,
147
161
  )
148
- result += f"returncode: {return_code}\nUse the get_lines tool to inspect the contents of these files when needed."
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
+ )
149
166
  return result
150
167
  except Exception as e:
151
- self.report_error(f" ❌ Error: {e}")
152
- return f"Error running command: {e}"
153
- # No temp script file to clean up anymore
168
+ self.report_error(tr(" ❌ Error: {error}", error=e))
169
+ return tr("Error running command: {error}", error=e)