janito 3.16.1__py3-none-any.whl → 3.16.3__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.
@@ -1,170 +1,169 @@
1
- """
2
- CLI Command: Show the resolved system prompt for the main agent (single-shot mode)
3
-
4
- Supports --profile to select a profile-specific system prompt template.
5
- """
6
-
7
- from janito.cli.core.runner import prepare_llm_driver_config
8
- from janito.platform_discovery import PlatformDiscovery
9
- from pathlib import Path
10
- from jinja2 import Template
11
- import importlib.resources
12
- import importlib.resources as resources
13
- import re
14
-
15
-
16
- def _compute_permission_string(args):
17
- from janito.tools.tool_base import ToolPermissions
18
-
19
- read = getattr(args, "read", False)
20
- write = getattr(args, "write", False)
21
- execute = getattr(args, "exec", False)
22
- allowed = ToolPermissions(read=read, write=write, execute=execute)
23
- perm_str = ""
24
- if allowed.read:
25
- perm_str += "r"
26
- if allowed.write:
27
- perm_str += "w"
28
- if allowed.execute:
29
- perm_str += "x"
30
- return perm_str or None
31
-
32
-
33
- def _prepare_context(args, agent_role, allowed_permissions):
34
- context = {}
35
- context["role"] = agent_role or "developer"
36
- context["profile"] = getattr(args, "profile", None)
37
- context["allowed_permissions"] = allowed_permissions
38
- context["emoji_enabled"] = getattr(args, "emoji", False)
39
- if allowed_permissions and "x" in allowed_permissions:
40
- pd = PlatformDiscovery()
41
- context["platform"] = pd.get_platform_name()
42
- context["python_version"] = pd.get_python_version()
43
- context["shell_info"] = pd.detect_shell()
44
- # Add Linux distro info if on Linux
45
- if pd.is_linux():
46
- context["linux_distro"] = pd.get_linux_distro()
47
- context["distro_info"] = pd.get_distro_info()
48
- return context
49
-
50
-
51
- def _load_template(profile, templates_dir):
52
- if profile:
53
- sanitized_profile = re.sub(r"\\s+", "_", profile.strip())
54
- template_filename = f"system_prompt_template_{sanitized_profile}.txt.j2"
55
- template_path = templates_dir / template_filename
56
- else:
57
- return None, None
58
- template_content = None
59
- if template_path and template_path.exists():
60
- with open(template_path, "r", encoding="utf-8") as file:
61
- template_content = file.read()
62
- else:
63
- try:
64
- with importlib.resources.files("janito.agent.templates.profiles").joinpath(
65
- template_filename
66
- ).open("r", encoding="utf-8") as file:
67
- template_content = file.read()
68
- except (FileNotFoundError, ModuleNotFoundError, AttributeError):
69
- # Also check user profiles directory
70
- from pathlib import Path
71
- import os
72
-
73
- user_profiles_dir = Path(os.path.expanduser("~/.janito/profiles"))
74
- user_template_path = user_profiles_dir / template_filename
75
- if user_template_path.exists():
76
- with open(user_template_path, "r", encoding="utf-8") as file:
77
- template_content = file.read()
78
- else:
79
- template_content = None
80
- return template_filename, template_content
81
- return template_filename, template_content
82
-
83
-
84
- def _print_debug_info(debug_flag, template_filename, allowed_permissions, context):
85
- if debug_flag:
86
- from rich import print as rich_print
87
-
88
- rich_print(
89
- f"[bold magenta][DEBUG][/bold magenta] Rendering system prompt template '[cyan]{template_filename}[/cyan]' with allowed_permissions: [yellow]{allowed_permissions}[/yellow]"
90
- )
91
- rich_print(
92
- f"[bold magenta][DEBUG][/bold magenta] Template context: [green]{context}[/green]"
93
- )
94
-
95
-
96
- def handle_show_system_prompt(args):
97
- from janito.cli.main_cli import MODIFIER_KEYS
98
-
99
- modifiers = {
100
- k: getattr(args, k) for k in MODIFIER_KEYS if getattr(args, k, None) is not None
101
- }
102
- provider, llm_driver_config, agent_role = prepare_llm_driver_config(args, modifiers)
103
- if provider is None or llm_driver_config is None:
104
- print("Error: Could not resolve provider or LLM driver config.")
105
- return
106
-
107
- allowed_permissions = _compute_permission_string(args)
108
- context = _prepare_context(args, agent_role, allowed_permissions)
109
-
110
- # Debug flag detection
111
- import sys
112
-
113
- debug_flag = False
114
- try:
115
- debug_flag = hasattr(sys, "argv") and (
116
- "--debug" in sys.argv or "--verbose" in sys.argv or "-v" in sys.argv
117
- )
118
- except Exception:
119
- pass
120
-
121
- templates_dir = (
122
- Path(__file__).parent.parent.parent / "agent" / "templates" / "profiles"
123
- )
124
- profile = getattr(args, "profile", None)
125
-
126
- # Handle --market flag mapping to Market Analyst profile
127
- if profile is None and getattr(args, "market", False):
128
- profile = "Market Analyst"
129
-
130
- # Handle --developer flag mapping to Developer profile
131
- if profile is None and getattr(args, "developer", False):
132
- profile = "Developer"
133
-
134
- if not profile:
135
- print(
136
- "[janito] No profile specified. The main agent runs without a system prompt template.\n"
137
- "Use --profile PROFILE to view a profile-specific system prompt."
138
- )
139
- return
140
-
141
- template_filename, template_content = _load_template(profile, templates_dir)
142
- _print_debug_info(debug_flag, template_filename, allowed_permissions, context)
143
-
144
- if not template_content:
145
- # Try to load directly from package resources as fallback
146
- try:
147
- template_content = (
148
- resources.files("janito.agent.templates.profiles")
149
- .joinpath(
150
- f"system_prompt_template_{profile.lower().replace(' ', '_')}.txt.j2"
151
- )
152
- .read_text(encoding="utf-8")
153
- )
154
- except (FileNotFoundError, ModuleNotFoundError, AttributeError):
155
- print(
156
- f"[janito] Could not find profile '{profile}'. This may be a configuration issue."
157
- )
158
- return
159
-
160
- template = Template(template_content)
161
- system_prompt = template.render(**context)
162
- system_prompt = re.sub(r"\n{3,}", "\n\n", system_prompt)
163
-
164
- # Use the actual profile name for display, not the resolved value
165
- display_profile = profile or "main"
166
- print(f"\n--- System Prompt (resolved, profile: {display_profile}) ---\n")
167
- print(system_prompt)
168
- print("\n-------------------------------\n")
169
- if agent_role:
170
- print(f"[Role: {agent_role}]")
1
+ """
2
+ CLI Command: Show the resolved system prompt for the main agent (single-shot mode)
3
+
4
+ Supports --profile to select a profile-specific system prompt template.
5
+ """
6
+
7
+ from janito.cli.core.runner import prepare_llm_driver_config
8
+ from janito.platform_discovery import PlatformDiscovery
9
+ from pathlib import Path
10
+ from jinja2 import Template
11
+ import importlib.resources
12
+ import importlib.resources as resources
13
+ import re
14
+
15
+
16
+ def _compute_permission_string(args):
17
+ from janito.tools.tool_base import ToolPermissions
18
+
19
+ read = getattr(args, "read", False)
20
+ write = getattr(args, "write", False)
21
+ execute = getattr(args, "exec", False)
22
+ allowed = ToolPermissions(read=read, write=write, execute=execute)
23
+ perm_str = ""
24
+ if allowed.read:
25
+ perm_str += "r"
26
+ if allowed.write:
27
+ perm_str += "w"
28
+ if allowed.execute:
29
+ perm_str += "x"
30
+ return perm_str or None
31
+
32
+
33
+ def _prepare_context(args, agent_role, allowed_permissions):
34
+ context = {}
35
+ context["role"] = agent_role or "developer"
36
+ context["profile"] = getattr(args, "profile", None)
37
+ context["allowed_permissions"] = allowed_permissions
38
+ if allowed_permissions and "x" in allowed_permissions:
39
+ pd = PlatformDiscovery()
40
+ context["platform"] = pd.get_platform_name()
41
+ context["python_version"] = pd.get_python_version()
42
+ context["shell_info"] = pd.detect_shell()
43
+ # Add Linux distro info if on Linux
44
+ if pd.is_linux():
45
+ context["linux_distro"] = pd.get_linux_distro()
46
+ context["distro_info"] = pd.get_distro_info()
47
+ return context
48
+
49
+
50
+ def _load_template(profile, templates_dir):
51
+ if profile:
52
+ sanitized_profile = re.sub(r"\\s+", "_", profile.strip())
53
+ template_filename = f"system_prompt_template_{sanitized_profile}.txt.j2"
54
+ template_path = templates_dir / template_filename
55
+ else:
56
+ return None, None
57
+ template_content = None
58
+ if template_path and template_path.exists():
59
+ with open(template_path, "r", encoding="utf-8") as file:
60
+ template_content = file.read()
61
+ else:
62
+ try:
63
+ with importlib.resources.files("janito.agent.templates.profiles").joinpath(
64
+ template_filename
65
+ ).open("r", encoding="utf-8") as file:
66
+ template_content = file.read()
67
+ except (FileNotFoundError, ModuleNotFoundError, AttributeError):
68
+ # Also check user profiles directory
69
+ from pathlib import Path
70
+ import os
71
+
72
+ user_profiles_dir = Path(os.path.expanduser("~/.janito/profiles"))
73
+ user_template_path = user_profiles_dir / template_filename
74
+ if user_template_path.exists():
75
+ with open(user_template_path, "r", encoding="utf-8") as file:
76
+ template_content = file.read()
77
+ else:
78
+ template_content = None
79
+ return template_filename, template_content
80
+ return template_filename, template_content
81
+
82
+
83
+ def _print_debug_info(debug_flag, template_filename, allowed_permissions, context):
84
+ if debug_flag:
85
+ from rich import print as rich_print
86
+
87
+ rich_print(
88
+ f"[bold magenta][DEBUG][/bold magenta] Rendering system prompt template '[cyan]{template_filename}[/cyan]' with allowed_permissions: [yellow]{allowed_permissions}[/yellow]"
89
+ )
90
+ rich_print(
91
+ f"[bold magenta][DEBUG][/bold magenta] Template context: [green]{context}[/green]"
92
+ )
93
+
94
+
95
+ def handle_show_system_prompt(args):
96
+ from janito.cli.main_cli import MODIFIER_KEYS
97
+
98
+ modifiers = {
99
+ k: getattr(args, k) for k in MODIFIER_KEYS if getattr(args, k, None) is not None
100
+ }
101
+ provider, llm_driver_config, agent_role = prepare_llm_driver_config(args, modifiers)
102
+ if provider is None or llm_driver_config is None:
103
+ print("Error: Could not resolve provider or LLM driver config.")
104
+ return
105
+
106
+ allowed_permissions = _compute_permission_string(args)
107
+ context = _prepare_context(args, agent_role, allowed_permissions)
108
+
109
+ # Debug flag detection
110
+ import sys
111
+
112
+ debug_flag = False
113
+ try:
114
+ debug_flag = hasattr(sys, "argv") and (
115
+ "--debug" in sys.argv or "--verbose" in sys.argv or "-v" in sys.argv
116
+ )
117
+ except Exception:
118
+ pass
119
+
120
+ templates_dir = (
121
+ Path(__file__).parent.parent.parent / "agent" / "templates" / "profiles"
122
+ )
123
+ profile = getattr(args, "profile", None)
124
+
125
+ # Handle --market flag mapping to Market Analyst profile
126
+ if profile is None and getattr(args, "market", False):
127
+ profile = "Market Analyst"
128
+
129
+ # Handle --developer flag mapping to Developer profile
130
+ if profile is None and getattr(args, "developer", False):
131
+ profile = "Developer"
132
+
133
+ if not profile:
134
+ print(
135
+ "[janito] No profile specified. The main agent runs without a system prompt template.\n"
136
+ "Use --profile PROFILE to view a profile-specific system prompt."
137
+ )
138
+ return
139
+
140
+ template_filename, template_content = _load_template(profile, templates_dir)
141
+ _print_debug_info(debug_flag, template_filename, allowed_permissions, context)
142
+
143
+ if not template_content:
144
+ # Try to load directly from package resources as fallback
145
+ try:
146
+ template_content = (
147
+ resources.files("janito.agent.templates.profiles")
148
+ .joinpath(
149
+ f"system_prompt_template_{profile.lower().replace(' ', '_')}.txt.j2"
150
+ )
151
+ .read_text(encoding="utf-8")
152
+ )
153
+ except (FileNotFoundError, ModuleNotFoundError, AttributeError):
154
+ print(
155
+ f"[janito] Could not find profile '{profile}'. This may be a configuration issue."
156
+ )
157
+ return
158
+
159
+ template = Template(template_content)
160
+ system_prompt = template.render(**context)
161
+ system_prompt = re.sub(r"\n{3,}", "\n\n", system_prompt)
162
+
163
+ # Use the actual profile name for display, not the resolved value
164
+ display_profile = profile or "main"
165
+ print(f"\n--- System Prompt (resolved, profile: {display_profile}) ---\n")
166
+ print(system_prompt)
167
+ print("\n-------------------------------\n")
168
+ if agent_role:
169
+ print(f"[Role: {agent_role}]")