janito 2.1.1__py3-none-any.whl → 2.3.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 +6 -6
- janito/agent/setup_agent.py +14 -5
- janito/agent/templates/profiles/system_prompt_template_main.txt.j2 +3 -1
- janito/cli/chat_mode/bindings.py +6 -0
- janito/cli/chat_mode/session.py +16 -0
- janito/cli/chat_mode/shell/autocomplete.py +21 -21
- janito/cli/chat_mode/shell/commands/__init__.py +3 -2
- janito/cli/chat_mode/shell/commands/clear.py +12 -12
- janito/cli/chat_mode/shell/commands/exec.py +27 -0
- janito/cli/chat_mode/shell/commands/multi.py +51 -51
- janito/cli/chat_mode/shell/commands/tools.py +17 -6
- janito/cli/chat_mode/shell/input_history.py +62 -62
- janito/cli/chat_mode/shell/session/manager.py +1 -0
- janito/cli/chat_mode/toolbar.py +3 -1
- janito/cli/cli_commands/list_models.py +35 -35
- janito/cli/cli_commands/list_providers.py +9 -9
- janito/cli/cli_commands/list_tools.py +53 -53
- janito/cli/cli_commands/model_selection.py +50 -50
- janito/cli/cli_commands/model_utils.py +13 -2
- janito/cli/cli_commands/set_api_key.py +19 -19
- janito/cli/cli_commands/show_config.py +51 -51
- janito/cli/cli_commands/show_system_prompt.py +62 -62
- janito/cli/config.py +2 -1
- janito/cli/core/__init__.py +4 -4
- janito/cli/core/event_logger.py +59 -59
- janito/cli/core/getters.py +3 -1
- janito/cli/core/runner.py +27 -6
- janito/cli/core/setters.py +5 -1
- janito/cli/core/unsetters.py +54 -54
- janito/cli/main_cli.py +12 -1
- janito/cli/prompt_core.py +5 -2
- janito/cli/rich_terminal_reporter.py +22 -3
- janito/cli/single_shot_mode/__init__.py +6 -6
- janito/cli/single_shot_mode/handler.py +11 -1
- janito/cli/verbose_output.py +1 -1
- janito/config.py +5 -5
- janito/config_manager.py +2 -0
- janito/driver_events.py +14 -0
- janito/drivers/anthropic/driver.py +113 -113
- janito/drivers/azure_openai/driver.py +38 -3
- janito/drivers/driver_registry.py +0 -2
- janito/drivers/openai/driver.py +196 -36
- janito/formatting_token.py +54 -54
- janito/i18n/__init__.py +35 -35
- janito/i18n/messages.py +23 -23
- janito/i18n/pt.py +47 -47
- janito/llm/__init__.py +5 -5
- janito/llm/agent.py +443 -443
- janito/llm/auth.py +1 -0
- janito/llm/driver.py +7 -1
- janito/llm/driver_config.py +1 -0
- janito/llm/driver_config_builder.py +34 -34
- janito/llm/driver_input.py +12 -12
- janito/llm/message_parts.py +60 -60
- janito/llm/model.py +38 -38
- janito/llm/provider.py +196 -196
- janito/provider_config.py +7 -3
- janito/provider_registry.py +29 -5
- janito/providers/__init__.py +1 -0
- janito/providers/anthropic/model_info.py +22 -22
- janito/providers/anthropic/provider.py +2 -2
- janito/providers/azure_openai/model_info.py +7 -6
- janito/providers/azure_openai/provider.py +44 -2
- janito/providers/deepseek/__init__.py +1 -1
- janito/providers/deepseek/model_info.py +16 -16
- janito/providers/deepseek/provider.py +91 -91
- janito/providers/google/model_info.py +21 -29
- janito/providers/google/provider.py +49 -38
- janito/providers/mistralai/provider.py +2 -2
- janito/providers/openai/model_info.py +0 -11
- janito/providers/openai/provider.py +1 -1
- janito/providers/provider_static_info.py +2 -3
- janito/providers/registry.py +26 -26
- janito/tools/adapters/__init__.py +1 -1
- janito/tools/adapters/local/__init__.py +62 -62
- janito/tools/adapters/local/adapter.py +33 -11
- janito/tools/adapters/local/ask_user.py +102 -102
- janito/tools/adapters/local/copy_file.py +84 -84
- janito/tools/adapters/local/create_directory.py +69 -69
- janito/tools/adapters/local/create_file.py +82 -82
- janito/tools/adapters/local/delete_text_in_file.py +4 -7
- janito/tools/adapters/local/fetch_url.py +97 -97
- janito/tools/adapters/local/find_files.py +138 -140
- janito/tools/adapters/local/get_file_outline/__init__.py +1 -1
- janito/tools/adapters/local/get_file_outline/core.py +117 -151
- janito/tools/adapters/local/get_file_outline/java_outline.py +40 -0
- janito/tools/adapters/local/get_file_outline/markdown_outline.py +14 -14
- janito/tools/adapters/local/get_file_outline/python_outline.py +303 -303
- janito/tools/adapters/local/get_file_outline/python_outline_v2.py +156 -156
- janito/tools/adapters/local/get_file_outline/search_outline.py +33 -33
- janito/tools/adapters/local/move_file.py +3 -13
- janito/tools/adapters/local/open_html_in_browser.py +24 -29
- janito/tools/adapters/local/open_url.py +3 -2
- janito/tools/adapters/local/python_code_run.py +166 -166
- janito/tools/adapters/local/python_command_run.py +164 -164
- janito/tools/adapters/local/python_file_run.py +163 -163
- janito/tools/adapters/local/remove_directory.py +6 -17
- janito/tools/adapters/local/remove_file.py +9 -15
- janito/tools/adapters/local/replace_text_in_file.py +6 -9
- janito/tools/adapters/local/run_bash_command.py +176 -176
- janito/tools/adapters/local/run_powershell_command.py +219 -219
- janito/tools/adapters/local/search_text/__init__.py +1 -1
- janito/tools/adapters/local/search_text/core.py +201 -201
- janito/tools/adapters/local/search_text/match_lines.py +1 -1
- janito/tools/adapters/local/search_text/pattern_utils.py +73 -73
- janito/tools/adapters/local/search_text/traverse_directory.py +145 -145
- janito/tools/adapters/local/validate_file_syntax/__init__.py +1 -1
- janito/tools/adapters/local/validate_file_syntax/core.py +106 -106
- janito/tools/adapters/local/validate_file_syntax/css_validator.py +35 -35
- janito/tools/adapters/local/validate_file_syntax/html_validator.py +93 -93
- janito/tools/adapters/local/validate_file_syntax/js_validator.py +27 -27
- janito/tools/adapters/local/validate_file_syntax/json_validator.py +6 -6
- janito/tools/adapters/local/validate_file_syntax/markdown_validator.py +109 -109
- janito/tools/adapters/local/validate_file_syntax/ps1_validator.py +32 -32
- janito/tools/adapters/local/validate_file_syntax/python_validator.py +5 -5
- janito/tools/adapters/local/validate_file_syntax/xml_validator.py +11 -11
- janito/tools/adapters/local/validate_file_syntax/yaml_validator.py +6 -6
- janito/tools/adapters/local/view_file.py +167 -167
- janito/tools/inspect_registry.py +17 -17
- janito/tools/tool_base.py +105 -105
- janito/tools/tool_events.py +58 -58
- janito/tools/tool_run_exception.py +12 -12
- janito/tools/tool_use_tracker.py +81 -81
- janito/tools/tool_utils.py +45 -45
- janito/tools/tools_adapter.py +78 -6
- janito/tools/tools_schema.py +104 -104
- janito/version.py +4 -4
- {janito-2.1.1.dist-info → janito-2.3.0.dist-info}/METADATA +388 -232
- janito-2.3.0.dist-info/RECORD +181 -0
- janito-2.3.0.dist-info/licenses/LICENSE +21 -0
- janito/cli/chat_mode/shell/commands/last.py +0 -137
- janito/drivers/google_genai/driver.py +0 -54
- janito/drivers/google_genai/schema_generator.py +0 -67
- janito-2.1.1.dist-info/RECORD +0 -181
- {janito-2.1.1.dist-info → janito-2.3.0.dist-info}/WHEEL +0 -0
- {janito-2.1.1.dist-info → janito-2.3.0.dist-info}/entry_points.txt +0 -0
- {janito-2.1.1.dist-info → janito-2.3.0.dist-info}/top_level.txt +0 -0
@@ -3,20 +3,35 @@ from janito.tools.tools_adapter import ToolsAdapterBase as ToolsAdapter
|
|
3
3
|
|
4
4
|
|
5
5
|
class LocalToolsAdapter(ToolsAdapter):
|
6
|
-
def
|
7
|
-
"""
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
def set_execution_tools_enabled(self, enabled: bool):
|
7
|
+
"""
|
8
|
+
Dynamically include or exclude execution tools from the enabled_tools set.
|
9
|
+
If enabled_tools is None, all tools are enabled (default). If set, restricts enabled tools.
|
10
|
+
"""
|
11
|
+
all_tool_names = set(self._tools.keys())
|
12
|
+
exec_tool_names = {
|
13
|
+
name for name, entry in self._tools.items()
|
14
|
+
if getattr(entry["instance"], "provides_execution", False)
|
15
|
+
}
|
16
|
+
if self._enabled_tools is None:
|
17
|
+
# If not restricted, create a new enabled-tools set excluding execution tools if disabling
|
18
|
+
if enabled:
|
19
|
+
self._enabled_tools = None # all tools enabled
|
20
|
+
else:
|
21
|
+
self._enabled_tools = all_tool_names - exec_tool_names
|
22
|
+
else:
|
23
|
+
if enabled:
|
24
|
+
self._enabled_tools |= exec_tool_names
|
25
|
+
else:
|
26
|
+
self._enabled_tools -= exec_tool_names
|
12
27
|
|
13
28
|
"""
|
14
29
|
Adapter for local, statically registered tools in the agent/tools system.
|
15
30
|
Handles registration, lookup, enabling/disabling, listing, and now, tool execution (merged from executor).
|
16
31
|
"""
|
17
32
|
|
18
|
-
def __init__(self, tools=None, event_bus=None,
|
19
|
-
super().__init__(tools=tools, event_bus=event_bus,
|
33
|
+
def __init__(self, tools=None, event_bus=None, enabled_tools=None, workdir=None):
|
34
|
+
super().__init__(tools=tools, event_bus=event_bus, enabled_tools=enabled_tools)
|
20
35
|
self._tools: Dict[str, Dict[str, Any]] = {}
|
21
36
|
self.workdir = workdir
|
22
37
|
if self.workdir:
|
@@ -56,13 +71,19 @@ class LocalToolsAdapter(ToolsAdapter):
|
|
56
71
|
return self._tools[name]["instance"] if name in self._tools else None
|
57
72
|
|
58
73
|
def list_tools(self):
|
59
|
-
|
74
|
+
if self._enabled_tools is None:
|
75
|
+
return list(self._tools.keys())
|
76
|
+
return [name for name in self._tools.keys() if name in self._enabled_tools]
|
60
77
|
|
61
78
|
def get_tool_classes(self):
|
62
|
-
|
79
|
+
if self._enabled_tools is None:
|
80
|
+
return [entry["class"] for entry in self._tools.values()]
|
81
|
+
return [entry["class"] for name, entry in self._tools.items() if name in self._enabled_tools]
|
63
82
|
|
64
83
|
def get_tools(self):
|
65
|
-
|
84
|
+
if self._enabled_tools is None:
|
85
|
+
return [entry["instance"] for entry in self._tools.values()]
|
86
|
+
return [entry["instance"] for name, entry in self._tools.items() if name in self._enabled_tools]
|
66
87
|
|
67
88
|
|
68
89
|
def add_tool(self, tool):
|
@@ -94,3 +115,4 @@ def register_local_tool(tool=None):
|
|
94
115
|
if tool is None:
|
95
116
|
return decorator
|
96
117
|
return decorator(tool)
|
118
|
+
|
@@ -1,102 +1,102 @@
|
|
1
|
-
from janito.tools.tool_base import ToolBase
|
2
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
3
|
-
|
4
|
-
from rich import print as rich_print
|
5
|
-
from janito.i18n import tr
|
6
|
-
from rich.panel import Panel
|
7
|
-
from prompt_toolkit import PromptSession
|
8
|
-
from prompt_toolkit.key_binding import KeyBindings
|
9
|
-
from prompt_toolkit.enums import EditingMode
|
10
|
-
from prompt_toolkit.formatted_text import HTML
|
11
|
-
from janito.cli.chat_mode.prompt_style import chat_shell_style
|
12
|
-
from prompt_toolkit.styles import Style
|
13
|
-
|
14
|
-
toolbar_style = Style.from_dict({"bottom-toolbar": "fg:yellow bg:darkred"})
|
15
|
-
|
16
|
-
|
17
|
-
@register_local_tool
|
18
|
-
class AskUserTool(ToolBase):
|
19
|
-
"""
|
20
|
-
Prompts the user for clarification or input with a question.
|
21
|
-
|
22
|
-
Args:
|
23
|
-
question (str): The question to ask the user.
|
24
|
-
|
25
|
-
Returns:
|
26
|
-
str: The user's response as a string. Example:
|
27
|
-
- "Yes"
|
28
|
-
- "No"
|
29
|
-
- "Some detailed answer..."
|
30
|
-
"""
|
31
|
-
|
32
|
-
tool_name = "ask_user"
|
33
|
-
|
34
|
-
def run(self, question: str) -> str:
|
35
|
-
|
36
|
-
print() # Print an empty line before the question panel
|
37
|
-
rich_print(Panel.fit(question, title=tr("Question"), style="cyan"))
|
38
|
-
|
39
|
-
bindings = KeyBindings()
|
40
|
-
mode = {"multiline": False}
|
41
|
-
|
42
|
-
@bindings.add("c-r")
|
43
|
-
def _(event):
|
44
|
-
pass
|
45
|
-
|
46
|
-
@bindings.add("f12")
|
47
|
-
def _(event):
|
48
|
-
buf = event.app.current_buffer
|
49
|
-
buf.text = "Do It"
|
50
|
-
buf.validate_and_handle()
|
51
|
-
|
52
|
-
# Use shared CLI styles
|
53
|
-
|
54
|
-
# prompt_style contains the prompt area and input background
|
55
|
-
# toolbar_style contains the bottom-toolbar styling
|
56
|
-
|
57
|
-
# Use the shared chat_shell_style for input styling only
|
58
|
-
style = chat_shell_style
|
59
|
-
|
60
|
-
def get_toolbar():
|
61
|
-
f12_hint = ""
|
62
|
-
if mode["multiline"]:
|
63
|
-
return HTML(
|
64
|
-
f"<b>Multiline mode (Esc+Enter to submit). Type /single to switch.</b>{f12_hint}"
|
65
|
-
)
|
66
|
-
else:
|
67
|
-
return HTML(
|
68
|
-
f"<b>Single-line mode (Enter to submit). Type /multi for multiline.</b>{f12_hint}"
|
69
|
-
)
|
70
|
-
|
71
|
-
session = PromptSession(
|
72
|
-
multiline=False,
|
73
|
-
key_bindings=bindings,
|
74
|
-
editing_mode=EditingMode.EMACS,
|
75
|
-
bottom_toolbar=get_toolbar,
|
76
|
-
style=style,
|
77
|
-
)
|
78
|
-
|
79
|
-
prompt_icon = HTML("<inputline>💬 </inputline>")
|
80
|
-
|
81
|
-
while True:
|
82
|
-
response = session.prompt(prompt_icon)
|
83
|
-
if not mode["multiline"] and response.strip() == "/multi":
|
84
|
-
mode["multiline"] = True
|
85
|
-
session.multiline = True
|
86
|
-
continue
|
87
|
-
elif mode["multiline"] and response.strip() == "/single":
|
88
|
-
mode["multiline"] = False
|
89
|
-
session.multiline = False
|
90
|
-
continue
|
91
|
-
else:
|
92
|
-
sanitized = response.strip()
|
93
|
-
try:
|
94
|
-
sanitized.encode("utf-8")
|
95
|
-
except UnicodeEncodeError:
|
96
|
-
sanitized = sanitized.encode("utf-8", errors="replace").decode(
|
97
|
-
"utf-8"
|
98
|
-
)
|
99
|
-
rich_print(
|
100
|
-
"[yellow]Warning: Some characters in your input were not valid UTF-8 and have been replaced.[/yellow]"
|
101
|
-
)
|
102
|
-
return sanitized
|
1
|
+
from janito.tools.tool_base import ToolBase
|
2
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
3
|
+
|
4
|
+
from rich import print as rich_print
|
5
|
+
from janito.i18n import tr
|
6
|
+
from rich.panel import Panel
|
7
|
+
from prompt_toolkit import PromptSession
|
8
|
+
from prompt_toolkit.key_binding import KeyBindings
|
9
|
+
from prompt_toolkit.enums import EditingMode
|
10
|
+
from prompt_toolkit.formatted_text import HTML
|
11
|
+
from janito.cli.chat_mode.prompt_style import chat_shell_style
|
12
|
+
from prompt_toolkit.styles import Style
|
13
|
+
|
14
|
+
toolbar_style = Style.from_dict({"bottom-toolbar": "fg:yellow bg:darkred"})
|
15
|
+
|
16
|
+
|
17
|
+
@register_local_tool
|
18
|
+
class AskUserTool(ToolBase):
|
19
|
+
"""
|
20
|
+
Prompts the user for clarification or input with a question.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
question (str): The question to ask the user.
|
24
|
+
|
25
|
+
Returns:
|
26
|
+
str: The user's response as a string. Example:
|
27
|
+
- "Yes"
|
28
|
+
- "No"
|
29
|
+
- "Some detailed answer..."
|
30
|
+
"""
|
31
|
+
|
32
|
+
tool_name = "ask_user"
|
33
|
+
|
34
|
+
def run(self, question: str) -> str:
|
35
|
+
|
36
|
+
print() # Print an empty line before the question panel
|
37
|
+
rich_print(Panel.fit(question, title=tr("Question"), style="cyan"))
|
38
|
+
|
39
|
+
bindings = KeyBindings()
|
40
|
+
mode = {"multiline": False}
|
41
|
+
|
42
|
+
@bindings.add("c-r")
|
43
|
+
def _(event):
|
44
|
+
pass
|
45
|
+
|
46
|
+
@bindings.add("f12")
|
47
|
+
def _(event):
|
48
|
+
buf = event.app.current_buffer
|
49
|
+
buf.text = "Do It"
|
50
|
+
buf.validate_and_handle()
|
51
|
+
|
52
|
+
# Use shared CLI styles
|
53
|
+
|
54
|
+
# prompt_style contains the prompt area and input background
|
55
|
+
# toolbar_style contains the bottom-toolbar styling
|
56
|
+
|
57
|
+
# Use the shared chat_shell_style for input styling only
|
58
|
+
style = chat_shell_style
|
59
|
+
|
60
|
+
def get_toolbar():
|
61
|
+
f12_hint = ""
|
62
|
+
if mode["multiline"]:
|
63
|
+
return HTML(
|
64
|
+
f"<b>Multiline mode (Esc+Enter to submit). Type /single to switch.</b>{f12_hint}"
|
65
|
+
)
|
66
|
+
else:
|
67
|
+
return HTML(
|
68
|
+
f"<b>Single-line mode (Enter to submit). Type /multi for multiline.</b>{f12_hint}"
|
69
|
+
)
|
70
|
+
|
71
|
+
session = PromptSession(
|
72
|
+
multiline=False,
|
73
|
+
key_bindings=bindings,
|
74
|
+
editing_mode=EditingMode.EMACS,
|
75
|
+
bottom_toolbar=get_toolbar,
|
76
|
+
style=style,
|
77
|
+
)
|
78
|
+
|
79
|
+
prompt_icon = HTML("<inputline>💬 </inputline>")
|
80
|
+
|
81
|
+
while True:
|
82
|
+
response = session.prompt(prompt_icon)
|
83
|
+
if not mode["multiline"] and response.strip() == "/multi":
|
84
|
+
mode["multiline"] = True
|
85
|
+
session.multiline = True
|
86
|
+
continue
|
87
|
+
elif mode["multiline"] and response.strip() == "/single":
|
88
|
+
mode["multiline"] = False
|
89
|
+
session.multiline = False
|
90
|
+
continue
|
91
|
+
else:
|
92
|
+
sanitized = response.strip()
|
93
|
+
try:
|
94
|
+
sanitized.encode("utf-8")
|
95
|
+
except UnicodeEncodeError:
|
96
|
+
sanitized = sanitized.encode("utf-8", errors="replace").decode(
|
97
|
+
"utf-8"
|
98
|
+
)
|
99
|
+
rich_print(
|
100
|
+
"[yellow]Warning: Some characters in your input were not valid UTF-8 and have been replaced.[/yellow]"
|
101
|
+
)
|
102
|
+
return sanitized
|
@@ -1,84 +1,84 @@
|
|
1
|
-
import os
|
2
|
-
import shutil
|
3
|
-
from typing import List, Union
|
4
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
5
|
-
from janito.tools.tool_base import ToolBase
|
6
|
-
from janito.tools.tool_utils import display_path
|
7
|
-
from janito.report_events import ReportAction
|
8
|
-
from janito.i18n import tr
|
9
|
-
|
10
|
-
|
11
|
-
@register_local_tool
|
12
|
-
class CopyFileTool(ToolBase):
|
13
|
-
"""
|
14
|
-
Copy one or more files to a target directory, or copy a single file to a new file.
|
15
|
-
Args:
|
16
|
-
sources (str): Space-separated path(s) to the file(s) to copy.
|
17
|
-
For multiple sources, provide a single string with paths separated by spaces.
|
18
|
-
target (str): Destination path. If copying multiple sources, this must be an existing directory.
|
19
|
-
overwrite (bool, optional): Overwrite existing files. Default: False.
|
20
|
-
Recommended only after reading the file to be overwritten.
|
21
|
-
Returns:
|
22
|
-
str: Status string for each copy operation.
|
23
|
-
"""
|
24
|
-
|
25
|
-
tool_name = "copy_file"
|
26
|
-
|
27
|
-
def run(self, sources: str, target: str, overwrite: bool = False) -> str:
|
28
|
-
source_list = [src for src in sources.split() if src]
|
29
|
-
messages = []
|
30
|
-
if len(source_list) > 1:
|
31
|
-
if not os.path.isdir(target):
|
32
|
-
return tr(
|
33
|
-
"❗ Target must be an existing directory when copying multiple files: '{target}'",
|
34
|
-
target=display_path(target),
|
35
|
-
)
|
36
|
-
for src in source_list:
|
37
|
-
if not os.path.isfile(src):
|
38
|
-
messages.append(
|
39
|
-
tr(
|
40
|
-
"❗ Source file does not exist: '{src}'",
|
41
|
-
src=display_path(src),
|
42
|
-
)
|
43
|
-
)
|
44
|
-
continue
|
45
|
-
dst = os.path.join(target, os.path.basename(src))
|
46
|
-
messages.append(self._copy_one(src, dst, overwrite=overwrite))
|
47
|
-
else:
|
48
|
-
src = source_list[0]
|
49
|
-
if os.path.isdir(target):
|
50
|
-
dst = os.path.join(target, os.path.basename(src))
|
51
|
-
else:
|
52
|
-
dst = target
|
53
|
-
messages.append(self._copy_one(src, dst, overwrite=overwrite))
|
54
|
-
return "\n".join(messages)
|
55
|
-
|
56
|
-
def _copy_one(self, src, dst, overwrite=False) -> str:
|
57
|
-
disp_src = display_path(src)
|
58
|
-
disp_dst = display_path(dst)
|
59
|
-
if not os.path.isfile(src):
|
60
|
-
return tr("❗ Source file does not exist: '{src}'", src=disp_src)
|
61
|
-
if os.path.exists(dst) and not overwrite:
|
62
|
-
return tr(
|
63
|
-
"❗ Target already exists: '{dst}'. Set overwrite=True to replace.",
|
64
|
-
dst=disp_dst,
|
65
|
-
)
|
66
|
-
try:
|
67
|
-
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
68
|
-
shutil.copy2(src, dst)
|
69
|
-
note = (
|
70
|
-
"\n⚠️ Overwrote existing file. (recommended only after reading the file to be overwritten)"
|
71
|
-
if (os.path.exists(dst) and overwrite)
|
72
|
-
else ""
|
73
|
-
)
|
74
|
-
self.report_success(
|
75
|
-
tr("✅ Copied '{src}' to '{dst}'", src=disp_src, dst=disp_dst)
|
76
|
-
)
|
77
|
-
return tr("✅ Copied '{src}' to '{dst}'", src=disp_src, dst=disp_dst) + note
|
78
|
-
except Exception as e:
|
79
|
-
return tr(
|
80
|
-
"❗ Copy failed from '{src}' to '{dst}': {err}",
|
81
|
-
src=disp_src,
|
82
|
-
dst=disp_dst,
|
83
|
-
err=str(e),
|
84
|
-
)
|
1
|
+
import os
|
2
|
+
import shutil
|
3
|
+
from typing import List, Union
|
4
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
5
|
+
from janito.tools.tool_base import ToolBase
|
6
|
+
from janito.tools.tool_utils import display_path
|
7
|
+
from janito.report_events import ReportAction
|
8
|
+
from janito.i18n import tr
|
9
|
+
|
10
|
+
|
11
|
+
@register_local_tool
|
12
|
+
class CopyFileTool(ToolBase):
|
13
|
+
"""
|
14
|
+
Copy one or more files to a target directory, or copy a single file to a new file.
|
15
|
+
Args:
|
16
|
+
sources (str): Space-separated path(s) to the file(s) to copy.
|
17
|
+
For multiple sources, provide a single string with paths separated by spaces.
|
18
|
+
target (str): Destination path. If copying multiple sources, this must be an existing directory.
|
19
|
+
overwrite (bool, optional): Overwrite existing files. Default: False.
|
20
|
+
Recommended only after reading the file to be overwritten.
|
21
|
+
Returns:
|
22
|
+
str: Status string for each copy operation.
|
23
|
+
"""
|
24
|
+
|
25
|
+
tool_name = "copy_file"
|
26
|
+
|
27
|
+
def run(self, sources: str, target: str, overwrite: bool = False) -> str:
|
28
|
+
source_list = [src for src in sources.split() if src]
|
29
|
+
messages = []
|
30
|
+
if len(source_list) > 1:
|
31
|
+
if not os.path.isdir(target):
|
32
|
+
return tr(
|
33
|
+
"❗ Target must be an existing directory when copying multiple files: '{target}'",
|
34
|
+
target=display_path(target),
|
35
|
+
)
|
36
|
+
for src in source_list:
|
37
|
+
if not os.path.isfile(src):
|
38
|
+
messages.append(
|
39
|
+
tr(
|
40
|
+
"❗ Source file does not exist: '{src}'",
|
41
|
+
src=display_path(src),
|
42
|
+
)
|
43
|
+
)
|
44
|
+
continue
|
45
|
+
dst = os.path.join(target, os.path.basename(src))
|
46
|
+
messages.append(self._copy_one(src, dst, overwrite=overwrite))
|
47
|
+
else:
|
48
|
+
src = source_list[0]
|
49
|
+
if os.path.isdir(target):
|
50
|
+
dst = os.path.join(target, os.path.basename(src))
|
51
|
+
else:
|
52
|
+
dst = target
|
53
|
+
messages.append(self._copy_one(src, dst, overwrite=overwrite))
|
54
|
+
return "\n".join(messages)
|
55
|
+
|
56
|
+
def _copy_one(self, src, dst, overwrite=False) -> str:
|
57
|
+
disp_src = display_path(src)
|
58
|
+
disp_dst = display_path(dst)
|
59
|
+
if not os.path.isfile(src):
|
60
|
+
return tr("❗ Source file does not exist: '{src}'", src=disp_src)
|
61
|
+
if os.path.exists(dst) and not overwrite:
|
62
|
+
return tr(
|
63
|
+
"❗ Target already exists: '{dst}'. Set overwrite=True to replace.",
|
64
|
+
dst=disp_dst,
|
65
|
+
)
|
66
|
+
try:
|
67
|
+
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
68
|
+
shutil.copy2(src, dst)
|
69
|
+
note = (
|
70
|
+
"\n⚠️ Overwrote existing file. (recommended only after reading the file to be overwritten)"
|
71
|
+
if (os.path.exists(dst) and overwrite)
|
72
|
+
else ""
|
73
|
+
)
|
74
|
+
self.report_success(
|
75
|
+
tr("✅ Copied '{src}' to '{dst}'", src=disp_src, dst=disp_dst)
|
76
|
+
)
|
77
|
+
return tr("✅ Copied '{src}' to '{dst}'", src=disp_src, dst=disp_dst) + note
|
78
|
+
except Exception as e:
|
79
|
+
return tr(
|
80
|
+
"❗ Copy failed from '{src}' to '{dst}': {err}",
|
81
|
+
src=disp_src,
|
82
|
+
dst=disp_dst,
|
83
|
+
err=str(e),
|
84
|
+
)
|
@@ -1,69 +1,69 @@
|
|
1
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
2
|
-
|
3
|
-
from janito.tools.tool_utils import display_path
|
4
|
-
from janito.tools.tool_base import ToolBase
|
5
|
-
from janito.report_events import ReportAction
|
6
|
-
from janito.i18n import tr
|
7
|
-
import os
|
8
|
-
|
9
|
-
|
10
|
-
@register_local_tool
|
11
|
-
class CreateDirectoryTool(ToolBase):
|
12
|
-
"""
|
13
|
-
Create a new directory at the specified file_path.
|
14
|
-
Args:
|
15
|
-
file_path (str): Path for the new directory.
|
16
|
-
Returns:
|
17
|
-
str: Status message indicating the result. Example:
|
18
|
-
- "5c5 Successfully created the directory at ..."
|
19
|
-
- "5d7 Cannot create directory: ..."
|
20
|
-
"""
|
21
|
-
|
22
|
-
tool_name = "create_directory"
|
23
|
-
|
24
|
-
def run(self, file_path: str) -> str:
|
25
|
-
# file_path = expand_path(file_path)
|
26
|
-
# Using file_path as is
|
27
|
-
disp_path = display_path(file_path)
|
28
|
-
self.report_action(
|
29
|
-
tr("📁 Create directory '{disp_path}' ...", disp_path=disp_path),
|
30
|
-
ReportAction.CREATE,
|
31
|
-
)
|
32
|
-
try:
|
33
|
-
if os.path.exists(file_path):
|
34
|
-
if not os.path.isdir(file_path):
|
35
|
-
self.report_error(
|
36
|
-
tr(
|
37
|
-
"❌ Path '{disp_path}' exists and is not a directory.",
|
38
|
-
disp_path=disp_path,
|
39
|
-
)
|
40
|
-
)
|
41
|
-
return tr(
|
42
|
-
"❌ Path '{disp_path}' exists and is not a directory.",
|
43
|
-
disp_path=disp_path,
|
44
|
-
)
|
45
|
-
self.report_error(
|
46
|
-
tr(
|
47
|
-
"❗ Directory '{disp_path}' already exists.",
|
48
|
-
disp_path=disp_path,
|
49
|
-
)
|
50
|
-
)
|
51
|
-
return tr(
|
52
|
-
"❗ Cannot create directory: '{disp_path}' already exists.",
|
53
|
-
disp_path=disp_path,
|
54
|
-
)
|
55
|
-
os.makedirs(file_path, exist_ok=True)
|
56
|
-
self.report_success(tr("✅ Directory created"))
|
57
|
-
return tr(
|
58
|
-
"✅ Successfully created the directory at '{disp_path}'.",
|
59
|
-
disp_path=disp_path,
|
60
|
-
)
|
61
|
-
except Exception as e:
|
62
|
-
self.report_error(
|
63
|
-
tr(
|
64
|
-
"❌ Error creating directory '{disp_path}': {error}",
|
65
|
-
disp_path=disp_path,
|
66
|
-
error=e,
|
67
|
-
)
|
68
|
-
)
|
69
|
-
return tr("❌ Cannot create directory: {error}", error=e)
|
1
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
2
|
+
|
3
|
+
from janito.tools.tool_utils import display_path
|
4
|
+
from janito.tools.tool_base import ToolBase
|
5
|
+
from janito.report_events import ReportAction
|
6
|
+
from janito.i18n import tr
|
7
|
+
import os
|
8
|
+
|
9
|
+
|
10
|
+
@register_local_tool
|
11
|
+
class CreateDirectoryTool(ToolBase):
|
12
|
+
"""
|
13
|
+
Create a new directory at the specified file_path.
|
14
|
+
Args:
|
15
|
+
file_path (str): Path for the new directory.
|
16
|
+
Returns:
|
17
|
+
str: Status message indicating the result. Example:
|
18
|
+
- "5c5 Successfully created the directory at ..."
|
19
|
+
- "5d7 Cannot create directory: ..."
|
20
|
+
"""
|
21
|
+
|
22
|
+
tool_name = "create_directory"
|
23
|
+
|
24
|
+
def run(self, file_path: str) -> str:
|
25
|
+
# file_path = expand_path(file_path)
|
26
|
+
# Using file_path as is
|
27
|
+
disp_path = display_path(file_path)
|
28
|
+
self.report_action(
|
29
|
+
tr("📁 Create directory '{disp_path}' ...", disp_path=disp_path),
|
30
|
+
ReportAction.CREATE,
|
31
|
+
)
|
32
|
+
try:
|
33
|
+
if os.path.exists(file_path):
|
34
|
+
if not os.path.isdir(file_path):
|
35
|
+
self.report_error(
|
36
|
+
tr(
|
37
|
+
"❌ Path '{disp_path}' exists and is not a directory.",
|
38
|
+
disp_path=disp_path,
|
39
|
+
)
|
40
|
+
)
|
41
|
+
return tr(
|
42
|
+
"❌ Path '{disp_path}' exists and is not a directory.",
|
43
|
+
disp_path=disp_path,
|
44
|
+
)
|
45
|
+
self.report_error(
|
46
|
+
tr(
|
47
|
+
"❗ Directory '{disp_path}' already exists.",
|
48
|
+
disp_path=disp_path,
|
49
|
+
)
|
50
|
+
)
|
51
|
+
return tr(
|
52
|
+
"❗ Cannot create directory: '{disp_path}' already exists.",
|
53
|
+
disp_path=disp_path,
|
54
|
+
)
|
55
|
+
os.makedirs(file_path, exist_ok=True)
|
56
|
+
self.report_success(tr("✅ Directory created"))
|
57
|
+
return tr(
|
58
|
+
"✅ Successfully created the directory at '{disp_path}'.",
|
59
|
+
disp_path=disp_path,
|
60
|
+
)
|
61
|
+
except Exception as e:
|
62
|
+
self.report_error(
|
63
|
+
tr(
|
64
|
+
"❌ Error creating directory '{disp_path}': {error}",
|
65
|
+
disp_path=disp_path,
|
66
|
+
error=e,
|
67
|
+
)
|
68
|
+
)
|
69
|
+
return tr("❌ Cannot create directory: {error}", error=e)
|