janito 1.14.2__py3-none-any.whl → 2.0.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 (282) hide show
  1. janito/__init__.py +6 -1
  2. janito/__main__.py +1 -1
  3. janito/agent/setup_agent.py +139 -0
  4. janito/agent/templates/profiles/{system_prompt_template_base.txt.j2 → system_prompt_template_main.txt.j2} +1 -1
  5. janito/cli/__init__.py +9 -0
  6. janito/cli/chat_mode/bindings.py +37 -0
  7. janito/cli/chat_mode/chat_entry.py +23 -0
  8. janito/cli/chat_mode/prompt_style.py +19 -0
  9. janito/cli/chat_mode/session.py +272 -0
  10. janito/{shell/prompt/completer.py → cli/chat_mode/shell/autocomplete.py} +7 -6
  11. janito/cli/chat_mode/shell/commands/__init__.py +55 -0
  12. janito/cli/chat_mode/shell/commands/base.py +9 -0
  13. janito/cli/chat_mode/shell/commands/clear.py +12 -0
  14. janito/{shell → cli/chat_mode/shell}/commands/conversation_restart.py +34 -30
  15. janito/cli/chat_mode/shell/commands/edit.py +25 -0
  16. janito/cli/chat_mode/shell/commands/help.py +16 -0
  17. janito/cli/chat_mode/shell/commands/history_view.py +93 -0
  18. janito/cli/chat_mode/shell/commands/lang.py +25 -0
  19. janito/cli/chat_mode/shell/commands/last.py +137 -0
  20. janito/cli/chat_mode/shell/commands/livelogs.py +49 -0
  21. janito/cli/chat_mode/shell/commands/multi.py +51 -0
  22. janito/cli/chat_mode/shell/commands/prompt.py +64 -0
  23. janito/cli/chat_mode/shell/commands/role.py +36 -0
  24. janito/cli/chat_mode/shell/commands/session.py +40 -0
  25. janito/{shell → cli/chat_mode/shell}/commands/session_control.py +2 -2
  26. janito/cli/chat_mode/shell/commands/termweb_log.py +92 -0
  27. janito/cli/chat_mode/shell/commands/tools.py +32 -0
  28. janito/{shell → cli/chat_mode/shell}/commands/utility.py +4 -7
  29. janito/{shell → cli/chat_mode/shell}/commands/verbose.py +5 -5
  30. janito/cli/chat_mode/shell/session/__init__.py +1 -0
  31. janito/{shell → cli/chat_mode/shell}/session/manager.py +9 -1
  32. janito/cli/chat_mode/toolbar.py +90 -0
  33. janito/cli/cli_commands/list_models.py +35 -0
  34. janito/cli/cli_commands/list_providers.py +9 -0
  35. janito/cli/cli_commands/list_tools.py +53 -0
  36. janito/cli/cli_commands/model_selection.py +50 -0
  37. janito/cli/cli_commands/model_utils.py +84 -0
  38. janito/cli/cli_commands/set_api_key.py +19 -0
  39. janito/cli/cli_commands/show_config.py +51 -0
  40. janito/cli/cli_commands/show_system_prompt.py +62 -0
  41. janito/cli/config.py +28 -0
  42. janito/cli/console.py +3 -0
  43. janito/cli/core/__init__.py +4 -0
  44. janito/cli/core/event_logger.py +59 -0
  45. janito/cli/core/getters.py +31 -0
  46. janito/cli/core/runner.py +141 -0
  47. janito/cli/core/setters.py +174 -0
  48. janito/cli/core/unsetters.py +54 -0
  49. janito/cli/main.py +8 -196
  50. janito/cli/main_cli.py +312 -0
  51. janito/cli/prompt_core.py +230 -0
  52. janito/cli/prompt_handler.py +6 -0
  53. janito/cli/rich_terminal_reporter.py +101 -0
  54. janito/cli/single_shot_mode/__init__.py +6 -0
  55. janito/cli/single_shot_mode/handler.py +137 -0
  56. janito/cli/termweb_starter.py +73 -24
  57. janito/cli/utils.py +25 -0
  58. janito/cli/verbose_output.py +196 -0
  59. janito/config.py +5 -0
  60. janito/config_manager.py +110 -0
  61. janito/conversation_history.py +30 -0
  62. janito/{agent/tools_utils/dir_walk_utils.py → dir_walk_utils.py} +3 -2
  63. janito/driver_events.py +98 -0
  64. janito/drivers/anthropic/driver.py +113 -0
  65. janito/drivers/azure_openai/driver.py +36 -0
  66. janito/drivers/driver_registry.py +33 -0
  67. janito/drivers/google_genai/driver.py +54 -0
  68. janito/drivers/google_genai/schema_generator.py +67 -0
  69. janito/drivers/mistralai/driver.py +41 -0
  70. janito/drivers/openai/driver.py +334 -0
  71. janito/event_bus/__init__.py +2 -0
  72. janito/event_bus/bus.py +68 -0
  73. janito/event_bus/event.py +15 -0
  74. janito/event_bus/handler.py +31 -0
  75. janito/event_bus/queue_bus.py +57 -0
  76. janito/exceptions.py +23 -0
  77. janito/formatting_token.py +54 -0
  78. janito/i18n/pt.py +1 -0
  79. janito/llm/__init__.py +5 -0
  80. janito/llm/agent.py +443 -0
  81. janito/llm/auth.py +62 -0
  82. janito/llm/driver.py +239 -0
  83. janito/llm/driver_config.py +34 -0
  84. janito/llm/driver_config_builder.py +34 -0
  85. janito/llm/driver_input.py +12 -0
  86. janito/llm/message_parts.py +60 -0
  87. janito/llm/model.py +38 -0
  88. janito/llm/provider.py +187 -0
  89. janito/perf_singleton.py +3 -0
  90. janito/performance_collector.py +167 -0
  91. janito/provider_config.py +98 -0
  92. janito/provider_registry.py +152 -0
  93. janito/providers/__init__.py +7 -0
  94. janito/providers/anthropic/model_info.py +22 -0
  95. janito/providers/anthropic/provider.py +65 -0
  96. janito/providers/azure_openai/model_info.py +15 -0
  97. janito/providers/azure_openai/provider.py +72 -0
  98. janito/providers/deepseek/__init__.py +1 -0
  99. janito/providers/deepseek/model_info.py +16 -0
  100. janito/providers/deepseek/provider.py +91 -0
  101. janito/providers/google/__init__.py +1 -0
  102. janito/providers/google/model_info.py +40 -0
  103. janito/providers/google/provider.py +69 -0
  104. janito/providers/mistralai/model_info.py +37 -0
  105. janito/providers/mistralai/provider.py +69 -0
  106. janito/providers/openai/__init__.py +1 -0
  107. janito/providers/openai/model_info.py +137 -0
  108. janito/providers/openai/provider.py +107 -0
  109. janito/providers/openai/schema_generator.py +63 -0
  110. janito/providers/provider_static_info.py +21 -0
  111. janito/providers/registry.py +26 -0
  112. janito/report_events.py +38 -0
  113. janito/termweb/app.py +1 -1
  114. janito/tools/__init__.py +16 -0
  115. janito/tools/adapters/__init__.py +1 -0
  116. janito/tools/adapters/local/__init__.py +54 -0
  117. janito/tools/adapters/local/adapter.py +92 -0
  118. janito/{agent/tools → tools/adapters/local}/ask_user.py +30 -13
  119. janito/tools/adapters/local/copy_file.py +84 -0
  120. janito/{agent/tools → tools/adapters/local}/create_directory.py +11 -10
  121. janito/tools/adapters/local/create_file.py +82 -0
  122. janito/tools/adapters/local/delete_text_in_file.py +136 -0
  123. janito/{agent/tools → tools/adapters/local}/fetch_url.py +18 -19
  124. janito/tools/adapters/local/find_files.py +140 -0
  125. janito/tools/adapters/local/get_file_outline/core.py +151 -0
  126. janito/{agent/tools → tools/adapters/local}/get_file_outline/python_outline.py +125 -0
  127. janito/tools/adapters/local/get_file_outline/python_outline_v2.py +156 -0
  128. janito/{agent/tools → tools/adapters/local}/get_file_outline/search_outline.py +12 -7
  129. janito/{agent/tools → tools/adapters/local}/move_file.py +13 -9
  130. janito/{agent/tools → tools/adapters/local}/open_url.py +7 -5
  131. janito/tools/adapters/local/python_code_run.py +165 -0
  132. janito/tools/adapters/local/python_command_run.py +163 -0
  133. janito/tools/adapters/local/python_file_run.py +162 -0
  134. janito/{agent/tools → tools/adapters/local}/remove_directory.py +15 -9
  135. janito/{agent/tools → tools/adapters/local}/remove_file.py +17 -14
  136. janito/{agent/tools → tools/adapters/local}/replace_text_in_file.py +27 -22
  137. janito/tools/adapters/local/run_bash_command.py +176 -0
  138. janito/tools/adapters/local/run_powershell_command.py +219 -0
  139. janito/{agent/tools → tools/adapters/local}/search_text/core.py +32 -12
  140. janito/{agent/tools → tools/adapters/local}/search_text/match_lines.py +13 -4
  141. janito/{agent/tools → tools/adapters/local}/search_text/pattern_utils.py +12 -4
  142. janito/{agent/tools → tools/adapters/local}/search_text/traverse_directory.py +15 -2
  143. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/core.py +12 -11
  144. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/css_validator.py +1 -1
  145. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/html_validator.py +1 -1
  146. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/js_validator.py +1 -1
  147. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/json_validator.py +1 -1
  148. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/markdown_validator.py +1 -1
  149. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/ps1_validator.py +1 -1
  150. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/python_validator.py +1 -1
  151. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/xml_validator.py +1 -1
  152. janito/{agent/tools → tools/adapters/local}/validate_file_syntax/yaml_validator.py +1 -1
  153. janito/{agent/tools/get_lines.py → tools/adapters/local/view_file.py} +45 -27
  154. janito/tools/inspect_registry.py +17 -0
  155. janito/tools/tool_base.py +105 -0
  156. janito/tools/tool_events.py +58 -0
  157. janito/tools/tool_run_exception.py +12 -0
  158. janito/{agent → tools}/tool_use_tracker.py +2 -4
  159. janito/{agent/tools_utils/utils.py → tools/tool_utils.py} +18 -9
  160. janito/tools/tools_adapter.py +207 -0
  161. janito/tools/tools_schema.py +104 -0
  162. janito/utils.py +11 -0
  163. janito/version.py +4 -0
  164. janito-2.0.0.dist-info/METADATA +232 -0
  165. janito-2.0.0.dist-info/RECORD +180 -0
  166. janito/agent/__init__.py +0 -0
  167. janito/agent/api_exceptions.py +0 -4
  168. janito/agent/config.py +0 -147
  169. janito/agent/config_defaults.py +0 -12
  170. janito/agent/config_utils.py +0 -0
  171. janito/agent/content_handler.py +0 -0
  172. janito/agent/conversation.py +0 -238
  173. janito/agent/conversation_api.py +0 -306
  174. janito/agent/conversation_exceptions.py +0 -18
  175. janito/agent/conversation_tool_calls.py +0 -39
  176. janito/agent/conversation_ui.py +0 -17
  177. janito/agent/event.py +0 -24
  178. janito/agent/event_dispatcher.py +0 -24
  179. janito/agent/event_handler_protocol.py +0 -5
  180. janito/agent/event_system.py +0 -15
  181. janito/agent/llm_conversation_history.py +0 -82
  182. janito/agent/message_handler.py +0 -20
  183. janito/agent/message_handler_protocol.py +0 -5
  184. janito/agent/openai_client.py +0 -149
  185. janito/agent/openai_schema_generator.py +0 -187
  186. janito/agent/profile_manager.py +0 -96
  187. janito/agent/queued_message_handler.py +0 -50
  188. janito/agent/rich_live.py +0 -32
  189. janito/agent/rich_message_handler.py +0 -115
  190. janito/agent/runtime_config.py +0 -36
  191. janito/agent/test_handler_protocols.py +0 -47
  192. janito/agent/test_openai_schema_generator.py +0 -93
  193. janito/agent/tests/__init__.py +0 -1
  194. janito/agent/tool_base.py +0 -63
  195. janito/agent/tool_executor.py +0 -122
  196. janito/agent/tool_registry.py +0 -49
  197. janito/agent/tools/__init__.py +0 -47
  198. janito/agent/tools/create_file.py +0 -59
  199. janito/agent/tools/delete_text_in_file.py +0 -97
  200. janito/agent/tools/find_files.py +0 -106
  201. janito/agent/tools/get_file_outline/core.py +0 -81
  202. janito/agent/tools/present_choices.py +0 -64
  203. janito/agent/tools/python_command_runner.py +0 -201
  204. janito/agent/tools/python_file_runner.py +0 -199
  205. janito/agent/tools/python_stdin_runner.py +0 -208
  206. janito/agent/tools/replace_file.py +0 -72
  207. janito/agent/tools/run_bash_command.py +0 -218
  208. janito/agent/tools/run_powershell_command.py +0 -251
  209. janito/agent/tools_utils/__init__.py +0 -1
  210. janito/agent/tools_utils/action_type.py +0 -7
  211. janito/agent/tools_utils/test_gitignore_utils.py +0 -46
  212. janito/cli/_livereload_log_utils.py +0 -13
  213. janito/cli/_print_config.py +0 -96
  214. janito/cli/_termweb_log_utils.py +0 -17
  215. janito/cli/_utils.py +0 -9
  216. janito/cli/arg_parser.py +0 -272
  217. janito/cli/cli_main.py +0 -281
  218. janito/cli/config_commands.py +0 -211
  219. janito/cli/config_runner.py +0 -35
  220. janito/cli/formatting_runner.py +0 -12
  221. janito/cli/livereload_starter.py +0 -60
  222. janito/cli/logging_setup.py +0 -38
  223. janito/cli/one_shot.py +0 -80
  224. janito/livereload/app.py +0 -25
  225. janito/rich_utils.py +0 -59
  226. janito/shell/__init__.py +0 -0
  227. janito/shell/commands/__init__.py +0 -61
  228. janito/shell/commands/config.py +0 -22
  229. janito/shell/commands/edit.py +0 -24
  230. janito/shell/commands/history_view.py +0 -18
  231. janito/shell/commands/lang.py +0 -19
  232. janito/shell/commands/livelogs.py +0 -42
  233. janito/shell/commands/prompt.py +0 -62
  234. janito/shell/commands/termweb_log.py +0 -94
  235. janito/shell/commands/tools.py +0 -26
  236. janito/shell/commands/track.py +0 -36
  237. janito/shell/main.py +0 -326
  238. janito/shell/prompt/load_prompt.py +0 -57
  239. janito/shell/prompt/session_setup.py +0 -57
  240. janito/shell/session/config.py +0 -109
  241. janito/shell/session/history.py +0 -0
  242. janito/shell/ui/interactive.py +0 -226
  243. janito/termweb/static/editor.css +0 -158
  244. janito/termweb/static/editor.css.bak +0 -145
  245. janito/termweb/static/editor.html +0 -46
  246. janito/termweb/static/editor.html.bak +0 -46
  247. janito/termweb/static/editor.js +0 -265
  248. janito/termweb/static/editor.js.bak +0 -259
  249. janito/termweb/static/explorer.html.bak +0 -59
  250. janito/termweb/static/favicon.ico +0 -0
  251. janito/termweb/static/favicon.ico.bak +0 -0
  252. janito/termweb/static/index.html +0 -53
  253. janito/termweb/static/index.html.bak +0 -54
  254. janito/termweb/static/index.html.bak.bak +0 -175
  255. janito/termweb/static/landing.html.bak +0 -36
  256. janito/termweb/static/termicon.svg +0 -1
  257. janito/termweb/static/termweb.css +0 -214
  258. janito/termweb/static/termweb.css.bak +0 -237
  259. janito/termweb/static/termweb.js +0 -162
  260. janito/termweb/static/termweb.js.bak +0 -168
  261. janito/termweb/static/termweb.js.bak.bak +0 -157
  262. janito/termweb/static/termweb_quickopen.js +0 -135
  263. janito/termweb/static/termweb_quickopen.js.bak +0 -125
  264. janito/tests/test_rich_utils.py +0 -44
  265. janito/web/__init__.py +0 -0
  266. janito/web/__main__.py +0 -25
  267. janito/web/app.py +0 -145
  268. janito-1.14.2.dist-info/METADATA +0 -306
  269. janito-1.14.2.dist-info/RECORD +0 -162
  270. janito-1.14.2.dist-info/licenses/LICENSE +0 -21
  271. /janito/{shell → cli/chat_mode/shell}/input_history.py +0 -0
  272. /janito/{shell/commands/session.py → cli/chat_mode/shell/session/history.py} +0 -0
  273. /janito/{agent/tools_utils/formatting.py → formatting.py} +0 -0
  274. /janito/{agent/tools_utils/gitignore_utils.py → gitignore_utils.py} +0 -0
  275. /janito/{agent/platform_discovery.py → platform_discovery.py} +0 -0
  276. /janito/{agent/tools → tools/adapters/local}/get_file_outline/__init__.py +0 -0
  277. /janito/{agent/tools → tools/adapters/local}/get_file_outline/markdown_outline.py +0 -0
  278. /janito/{agent/tools → tools/adapters/local}/search_text/__init__.py +0 -0
  279. /janito/{agent/tools → tools/adapters/local}/validate_file_syntax/__init__.py +0 -0
  280. {janito-1.14.2.dist-info → janito-2.0.0.dist-info}/WHEEL +0 -0
  281. {janito-1.14.2.dist-info → janito-2.0.0.dist-info}/entry_points.txt +0 -0
  282. {janito-1.14.2.dist-info → janito-2.0.0.dist-info}/top_level.txt +0 -0
@@ -1,16 +1,17 @@
1
1
  import requests
2
2
  from bs4 import BeautifulSoup
3
- from janito.agent.tool_registry import register_tool
4
- from janito.agent.tool_base import ToolBase
5
- from janito.agent.tools_utils.action_type import ActionType
3
+ from janito.tools.adapters.local.adapter import register_local_tool
4
+ from janito.tools.tool_base import ToolBase
5
+ from janito.report_events import ReportAction
6
6
  from janito.i18n import tr
7
- from janito.agent.tools_utils.utils import pluralize
7
+ from janito.tools.tool_utils import pluralize
8
8
 
9
9
 
10
- @register_tool(name="fetch_url")
10
+ @register_local_tool
11
11
  class FetchUrlTool(ToolBase):
12
12
  """
13
13
  Fetch the content of a web page and extract its text.
14
+
14
15
  Args:
15
16
  url (str): The URL of the web page to fetch.
16
17
  search_strings (list[str], optional): Strings to search for in the page content.
@@ -21,11 +22,13 @@ class FetchUrlTool(ToolBase):
21
22
  - "Warning: Empty URL provided. Operation skipped."
22
23
  """
23
24
 
25
+ tool_name = "fetch_url"
26
+
24
27
  def run(self, url: str, search_strings: list[str] = None) -> str:
25
28
  if not url.strip():
26
- self.report_warning(tr("ℹ️ Empty URL provided."))
29
+ self.report_warning(tr("ℹ️ Empty URL provided."), ReportAction.READ)
27
30
  return tr("Warning: Empty URL provided. Operation skipped.")
28
- self.report_info(ActionType.READ, tr("🌐 Fetch URL '{url}' ...", url=url))
31
+ self.report_action(tr("🌐 Fetch URL '{url}' ...", url=url), ReportAction.READ)
29
32
  try:
30
33
  response = requests.get(url, timeout=10)
31
34
  response.raise_for_status()
@@ -37,7 +40,8 @@ class FetchUrlTool(ToolBase):
37
40
  "❗ HTTP {status_code} error for URL: {url}",
38
41
  status_code=status_code,
39
42
  url=url,
40
- )
43
+ ),
44
+ ReportAction.READ,
41
45
  )
42
46
  return tr(
43
47
  "Warning: HTTP {status_code} error for URL: {url}",
@@ -50,7 +54,8 @@ class FetchUrlTool(ToolBase):
50
54
  "❗ HTTP error for URL: {url}: {err}",
51
55
  url=url,
52
56
  err=str(http_err),
53
- )
57
+ ),
58
+ ReportAction.READ,
54
59
  )
55
60
  return tr(
56
61
  "Warning: HTTP error for URL: {url}: {err}",
@@ -59,19 +64,12 @@ class FetchUrlTool(ToolBase):
59
64
  )
60
65
  except Exception as err:
61
66
  self.report_error(
62
- tr("❗ Error fetching URL: {url}: {err}", url=url, err=str(err))
67
+ tr("❗ Error fetching URL: {url}: {err}", url=url, err=str(err)),
68
+ ReportAction.READ,
63
69
  )
64
70
  return tr(
65
71
  "Warning: Error fetching URL: {url}: {err}", url=url, err=str(err)
66
72
  )
67
- self.update_progress(
68
- {
69
- "event": "progress",
70
- "message": tr(
71
- "Fetched URL with status {status}", status=response.status_code
72
- ),
73
- }
74
- )
75
73
  soup = BeautifulSoup(response.text, "html.parser")
76
74
  text = soup.get_text(separator="\n")
77
75
  if search_strings:
@@ -93,6 +91,7 @@ class FetchUrlTool(ToolBase):
93
91
  "✅ {num_lines} {line_word}",
94
92
  num_lines=num_lines,
95
93
  line_word=pluralize("line", num_lines),
96
- )
94
+ ),
95
+ ReportAction.READ,
97
96
  )
98
97
  return text
@@ -0,0 +1,140 @@
1
+ from janito.tools.tool_base import ToolBase
2
+ from janito.report_events import ReportAction
3
+ from janito.tools.adapters.local.adapter import register_local_tool
4
+ from janito.tools.tool_utils import pluralize, display_path
5
+ from janito.dir_walk_utils import walk_dir_with_gitignore
6
+ from janito.i18n import tr
7
+ import fnmatch
8
+ import os
9
+
10
+
11
+ @register_local_tool
12
+ class FindFilesTool(ToolBase):
13
+ """
14
+ Find files or directories in one or more directories matching a pattern. Respects .gitignore.
15
+
16
+ If a path is an existing file, it is checked against the provided pattern(s) and included in the results if it matches. This allows find_files to be used to look for a specific set of filenames in a single call, as well as searching directories.
17
+
18
+ Args:
19
+ paths (str): String of one or more paths (space-separated) to search in. Each path can be a directory or a file.
20
+ pattern (str): File pattern(s) to match. Multiple patterns can be separated by spaces. Uses Unix shell-style wildcards (fnmatch), e.g. '*.py', 'data_??.csv', '[a-z]*.txt'.
21
+ - If the pattern ends with '/' or '\', only matching directory names (with trailing slash) are returned, not the files within those directories. For example, pattern '*/' will return only directories at the specified depth.
22
+ max_depth (int, optional): Maximum directory depth to search. If None, unlimited recursion. If 0, only the top-level directory. If 1, only the root directory (matches 'find . -maxdepth 1').
23
+ include_gitignored (bool, optional): If True, includes files/directories ignored by .gitignore. Defaults to False.
24
+ max_results (int, optional): Maximum number of results to return. 0 means no limit (default).
25
+ Returns:
26
+ str: Newline-separated list of matching file paths. Example:
27
+ "/path/to/file1.py\n/path/to/file2.py"
28
+ "Warning: Empty file pattern provided. Operation skipped."
29
+ If max_results is reached, appends a note to the output.
30
+ """
31
+
32
+ tool_name = "find_files"
33
+
34
+ def _match_directories(self, root, dirs, pat):
35
+ dir_output = set()
36
+ dir_pat = pat.rstrip("/\\")
37
+ for d in dirs:
38
+ if fnmatch.fnmatch(d, dir_pat):
39
+ dir_output.add(os.path.join(root, d) + os.sep)
40
+ return dir_output
41
+
42
+ def _match_files(self, root, files, pat):
43
+ file_output = set()
44
+ for filename in fnmatch.filter(files, pat):
45
+ file_output.add(os.path.join(root, filename))
46
+ return file_output
47
+
48
+ def _match_dirs_without_slash(self, root, dirs, pat):
49
+ dir_output = set()
50
+ for d in fnmatch.filter(dirs, pat):
51
+ dir_output.add(os.path.join(root, d))
52
+ return dir_output
53
+
54
+ def _handle_file_path(self, directory, patterns):
55
+ dir_output = set()
56
+ filename = os.path.basename(directory)
57
+ for pat in patterns:
58
+ # Only match files, not directories, for file paths
59
+ if not (pat.endswith("/") or pat.endswith("\\")):
60
+ if fnmatch.fnmatch(filename, pat):
61
+ dir_output.add(directory)
62
+ break
63
+ return dir_output
64
+
65
+ def _handle_directory_path(self, directory, patterns, max_depth, include_gitignored):
66
+ dir_output = set()
67
+ for root, dirs, files in walk_dir_with_gitignore(
68
+ directory,
69
+ max_depth=max_depth,
70
+ include_gitignored=include_gitignored,
71
+ ):
72
+ for pat in patterns:
73
+ if pat.endswith("/") or pat.endswith("\\"):
74
+ dir_output.update(self._match_directories(root, dirs, pat))
75
+ else:
76
+ dir_output.update(self._match_files(root, files, pat))
77
+ dir_output.update(
78
+ self._match_dirs_without_slash(root, dirs, pat)
79
+ )
80
+ return dir_output
81
+
82
+ def _report_search(self, pattern, disp_path, depth_msg):
83
+ self.report_action(
84
+ tr(
85
+ "🔍 Search for files '{pattern}' in '{disp_path}'{depth_msg} ...",
86
+ pattern=pattern,
87
+ disp_path=disp_path,
88
+ depth_msg=depth_msg,
89
+ ),
90
+ ReportAction.READ,
91
+ )
92
+
93
+ def _report_success(self, count):
94
+ self.report_success(
95
+ tr(
96
+ " ✅ {count} {file_word}",
97
+ count=count,
98
+ file_word=pluralize("file", count),
99
+ ),
100
+ ReportAction.READ,
101
+ )
102
+
103
+ def _format_output(self, directory, dir_output):
104
+ if directory.strip() == ".":
105
+ dir_output = {
106
+ p[2:] if (p.startswith("./") or p.startswith(".\\")) else p
107
+ for p in dir_output
108
+ }
109
+ return sorted(dir_output)
110
+
111
+ def run(
112
+ self,
113
+ paths: str,
114
+ pattern: str,
115
+ max_depth: int = None,
116
+ include_gitignored: bool = False,
117
+ ) -> str:
118
+ if not pattern:
119
+ self.report_warning(tr("ℹ️ Empty file pattern provided."), ReportAction.READ)
120
+ return tr("Warning: Empty file pattern provided. Operation skipped.")
121
+ patterns = pattern.split()
122
+ results = []
123
+ for directory in paths.split():
124
+ disp_path = display_path(directory)
125
+ depth_msg = (
126
+ tr(" (max depth: {max_depth})", max_depth=max_depth)
127
+ if max_depth is not None and max_depth > 0
128
+ else ""
129
+ )
130
+ self._report_search(pattern, disp_path, depth_msg)
131
+ dir_output = set()
132
+ if os.path.isfile(directory):
133
+ dir_output = self._handle_file_path(directory, patterns)
134
+ elif os.path.isdir(directory):
135
+ dir_output = self._handle_directory_path(directory, patterns, max_depth, include_gitignored)
136
+ self._report_success(len(dir_output))
137
+ results.extend(self._format_output(directory, dir_output))
138
+ result = "\n".join(results)
139
+ return result
140
+
@@ -0,0 +1,151 @@
1
+ from janito.tools.adapters.local.adapter import register_local_tool
2
+ from .python_outline import parse_python_outline
3
+ from .markdown_outline import parse_markdown_outline
4
+ from janito.formatting import OutlineFormatter
5
+ import os
6
+ from janito.tools.tool_base import ToolBase
7
+ from janito.report_events import ReportAction
8
+ from janito.tools.tool_utils import display_path, pluralize
9
+ from janito.i18n import tr
10
+
11
+ from janito.tools.adapters.local.adapter import register_local_tool as register_tool
12
+
13
+
14
+ @register_tool
15
+ class GetFileOutlineTool(ToolBase):
16
+ """
17
+ Get an outline of a file's structure. Supports Python and Markdown files.
18
+
19
+ Args:
20
+ file_path (str): Path to the file to outline.
21
+ """
22
+
23
+ tool_name = "get_file_outline"
24
+
25
+ def run(self, file_path: str) -> str:
26
+ try:
27
+ self.report_action(
28
+ tr(
29
+ "📄 Outline file '{disp_path}' ...",
30
+ disp_path=display_path(file_path),
31
+ ),
32
+ ReportAction.READ,
33
+ )
34
+ ext = os.path.splitext(file_path)[1].lower()
35
+ with open(file_path, "r", encoding="utf-8", errors="replace") as f:
36
+ lines = f.readlines()
37
+ if ext == ".py":
38
+ outline_items = parse_python_outline(lines)
39
+ outline_type = "python"
40
+ table = OutlineFormatter.format_outline_table(outline_items)
41
+ self.report_success(
42
+ tr(
43
+ "✅ Outlined {count} {item_word}",
44
+ count=len(outline_items),
45
+ item_word=pluralize("item", len(outline_items)),
46
+ ),
47
+ ReportAction.READ,
48
+ )
49
+ return (
50
+ tr(
51
+ "Outline: {count} items ({outline_type})\n",
52
+ count=len(outline_items),
53
+ outline_type=outline_type,
54
+ )
55
+ + table
56
+ )
57
+ elif ext == ".md":
58
+ outline_items = parse_markdown_outline(lines)
59
+ outline_type = "markdown"
60
+ table = OutlineFormatter.format_markdown_outline_table(outline_items)
61
+ self.report_success(
62
+ tr(
63
+ "✅ Outlined {count} {item_word}",
64
+ count=len(outline_items),
65
+ item_word=pluralize("item", len(outline_items)),
66
+ ),
67
+ ReportAction.READ,
68
+ )
69
+ return (
70
+ tr(
71
+ "Outline: {count} items ({outline_type})\n",
72
+ count=len(outline_items),
73
+ outline_type=outline_type,
74
+ )
75
+ + table
76
+ )
77
+ else:
78
+ outline_type = "default"
79
+ self.report_success(
80
+ tr("✅ Outlined {count} items", count=len(lines)),
81
+ ReportAction.READ,
82
+ )
83
+ return tr(
84
+ "Outline: {count} lines ({outline_type})\nFile has {count} lines.",
85
+ count=len(lines),
86
+ outline_type=outline_type,
87
+ )
88
+ except Exception as e:
89
+ self.report_error(
90
+ tr("❌ Error reading file: {error}", error=e),
91
+ ReportAction.OUTLINE,
92
+ )
93
+ return tr("Error reading file: {error}", error=e)
94
+
95
+ if ext == ".py":
96
+ outline_items = parse_python_outline(lines)
97
+ outline_type = "python"
98
+ table = OutlineFormatter.format_outline_table(outline_items)
99
+ self.report_success(
100
+ tr(
101
+ "✅ Outlined {count} {item_word}",
102
+ count=len(outline_items),
103
+ item_word=pluralize("item", len(outline_items)),
104
+ ),
105
+ ReportAction.READ,
106
+ )
107
+ return (
108
+ tr(
109
+ "Outline: {count} items ({outline_type})\n",
110
+ count=len(outline_items),
111
+ outline_type=outline_type,
112
+ )
113
+ + table
114
+ )
115
+ elif ext == ".md":
116
+ outline_items = parse_markdown_outline(lines)
117
+ outline_type = "markdown"
118
+ table = OutlineFormatter.format_markdown_outline_table(outline_items)
119
+ self.report_success(
120
+ tr(
121
+ "✅ Outlined {count} {item_word}",
122
+ count=len(outline_items),
123
+ item_word=pluralize("item", len(outline_items)),
124
+ ),
125
+ ReportAction.READ,
126
+ )
127
+ return (
128
+ tr(
129
+ "Outline: {count} items ({outline_type})\n",
130
+ count=len(outline_items),
131
+ outline_type=outline_type,
132
+ )
133
+ + table
134
+ )
135
+ else:
136
+ outline_type = "default"
137
+ self.report_success(
138
+ tr("✅ Outlined {count} items", count=len(lines)),
139
+ ReportAction.READ,
140
+ )
141
+ return tr(
142
+ "Outline: {count} lines ({outline_type})\nFile has {count} lines.",
143
+ count=len(lines),
144
+ outline_type=outline_type,
145
+ )
146
+ except Exception as e:
147
+ self.report_error(
148
+ tr("❌ Error reading file: {error}", error=e),
149
+ ReportAction.OUTLINE,
150
+ )
151
+ return tr("Error reading file: {error}", error=e)
@@ -99,6 +99,57 @@ def process_line(idx, line, regexes, stack, obj_ranges, outline, last_top_obj):
99
99
  return last_top_obj
100
100
 
101
101
 
102
+ def extract_signature_and_decorators(lines, start_idx):
103
+ """
104
+ Extracts the signature line and leading decorators for a given function/class/method.
105
+ Returns (signature:str, decorators:List[str], signature_lineno:int)
106
+ """
107
+ decorators = []
108
+ sig_line = None
109
+ sig_lineno = start_idx
110
+ for i in range(start_idx - 1, -1, -1):
111
+ striped = lines[i].strip()
112
+ if striped.startswith("@"):
113
+ decorators.insert(0, striped)
114
+ sig_lineno = i
115
+ elif not striped:
116
+ continue
117
+ else:
118
+ break
119
+ # Find the signature line itself
120
+ for k in range(start_idx, len(lines)):
121
+ striped = lines[k].strip()
122
+ if striped.startswith("def ") or striped.startswith("class "):
123
+ sig_line = striped
124
+ sig_lineno = k
125
+ break
126
+ return sig_line, decorators, sig_lineno
127
+
128
+
129
+ def extract_docstring(lines, start_idx, end_idx):
130
+ """Extracts a docstring from lines[start_idx:end_idx] if present."""
131
+ for i in range(start_idx, min(end_idx, len(lines))):
132
+ line = lines[i].lstrip()
133
+ if not line:
134
+ continue
135
+ if line.startswith('"""') or line.startswith("'''"):
136
+ quote = line[:3]
137
+ doc = line[3:]
138
+ if doc.strip().endswith(quote):
139
+ return doc.strip()[:-3].strip()
140
+ docstring_lines = [doc]
141
+ for j in range(i + 1, min(end_idx, len(lines))):
142
+ line = lines[j]
143
+ if line.strip().endswith(quote):
144
+ docstring_lines.append(line.strip()[:-3])
145
+ return "\n".join([d.strip() for d in docstring_lines]).strip()
146
+ docstring_lines.append(line)
147
+ break
148
+ else:
149
+ break
150
+ return ""
151
+
152
+
102
153
  def build_outline_entry(obj, lines, outline):
103
154
  obj_type, name, start, end, parent, indent = obj
104
155
  # Determine if this is a method
@@ -145,6 +196,80 @@ def build_outline(obj_ranges, lines, outline):
145
196
 
146
197
 
147
198
  def parse_python_outline(lines: List[str]):
199
+ class_pat = re.compile(r"^(\s*)class\s+(\w+)")
200
+ func_pat = re.compile(r"^(\s*)def\s+(\w+)")
201
+ assign_pat = re.compile(r"^(\s*)([A-Za-z_][A-Za-z0-9_]*)\s*=.*")
202
+ main_pat = re.compile(r"^\s*if\s+__name__\s*==\s*[\'\"]__main__[\'\"]\s*:")
203
+ outline = []
204
+ stack = []
205
+ obj_ranges = []
206
+ last_top_obj = None
207
+ for idx, line in enumerate(lines):
208
+ class_match = class_pat.match(line)
209
+ func_match = func_pat.match(line)
210
+ assign_match = assign_pat.match(line)
211
+ indent = len(line) - len(line.lstrip())
212
+ parent = ""
213
+ for s in reversed(stack):
214
+ if s[0] == "class" and indent > s[2]:
215
+ parent = s[1]
216
+ break
217
+ if class_match:
218
+ obj = ("class", class_match.group(2), idx + 1, None, parent, indent)
219
+ stack.append(obj)
220
+ last_top_obj = obj
221
+ elif func_match:
222
+ obj = ("function", func_match.group(2), idx + 1, None, parent, indent)
223
+ stack.append(obj)
224
+ last_top_obj = obj
225
+ elif assign_match and indent == 0:
226
+ outline.append(
227
+ {
228
+ "type": "const" if assign_match.group(2).isupper() else "var",
229
+ "name": assign_match.group(2),
230
+ "start": idx + 1,
231
+ "end": idx + 1,
232
+ "parent": "",
233
+ "signature": line.strip(),
234
+ "decorators": [],
235
+ "docstring": "",
236
+ }
237
+ )
238
+ if line.strip().startswith("if __name__ == "):
239
+ outline.append(
240
+ {
241
+ "type": "main",
242
+ "name": "__main__",
243
+ "start": idx + 1,
244
+ "end": idx + 1,
245
+ "parent": "",
246
+ "signature": line.strip(),
247
+ "decorators": [],
248
+ "docstring": "",
249
+ }
250
+ )
251
+ # Close stack objects if indent falls back
252
+ while stack and indent <= stack[-1][5] and idx + 1 > stack[-1][2]:
253
+ finished = stack.pop()
254
+ outline_entry = finished[:2] + (
255
+ finished[2],
256
+ idx + 1,
257
+ finished[4],
258
+ finished[5],
259
+ )
260
+ build_outline_entry(outline_entry, lines, outline)
261
+ # Close any remaining objects
262
+ while stack:
263
+ finished = stack.pop()
264
+ outline_entry = finished[:2] + (
265
+ finished[2],
266
+ len(lines),
267
+ finished[4],
268
+ finished[5],
269
+ )
270
+ build_outline_entry(outline_entry, lines, outline)
271
+ return outline
272
+
148
273
  class_pat = re.compile(r"^(\s*)class\s+(\w+)")
149
274
  func_pat = re.compile(r"^(\s*)def\s+(\w+)")
150
275
  assign_pat = re.compile(r"^(\s*)([A-Za-z_][A-Za-z0-9_]*)\s*=.*")
@@ -0,0 +1,156 @@
1
+ import re
2
+ from typing import List
3
+
4
+
5
+ def extract_signature_and_decorators(lines, start_idx):
6
+ """
7
+ Extracts the signature line and leading decorators for a given function/class/method.
8
+ Returns (signature:str, decorators:List[str], signature_lineno:int)
9
+ """
10
+ decorators = []
11
+ sig_line = None
12
+ sig_lineno = start_idx
13
+ for i in range(start_idx - 1, -1, -1):
14
+ striped = lines[i].strip()
15
+ if striped.startswith("@"):
16
+ decorators.insert(0, striped)
17
+ sig_lineno = i
18
+ elif not striped:
19
+ continue
20
+ else:
21
+ break
22
+ # Find the signature line itself
23
+ for k in range(start_idx, len(lines)):
24
+ striped = lines[k].strip()
25
+ if striped.startswith("def ") or striped.startswith("class "):
26
+ sig_line = striped
27
+ sig_lineno = k
28
+ break
29
+ return sig_line, decorators, sig_lineno
30
+
31
+
32
+ def extract_docstring(lines, start_idx, end_idx):
33
+ """Extracts a docstring from lines[start_idx:end_idx] if present."""
34
+ for i in range(start_idx, min(end_idx, len(lines))):
35
+ line = lines[i].lstrip()
36
+ if not line:
37
+ continue
38
+ if line.startswith('"""') or line.startswith("'''"):
39
+ quote = line[:3]
40
+ doc = line[3:]
41
+ if doc.strip().endswith(quote):
42
+ return doc.strip()[:-3].strip()
43
+ docstring_lines = [doc]
44
+ for j in range(i + 1, min(end_idx, len(lines))):
45
+ line = lines[j]
46
+ if line.strip().endswith(quote):
47
+ docstring_lines.append(line.strip()[:-3])
48
+ return "\n".join([d.strip() for d in docstring_lines]).strip()
49
+ docstring_lines.append(line)
50
+ break
51
+ else:
52
+ break
53
+ return ""
54
+
55
+
56
+ def build_outline_entry(obj, lines, outline):
57
+ obj_type, name, start, end, parent, indent = obj
58
+ # Determine if this is a method
59
+ if obj_type == "function" and parent:
60
+ outline_type = "method"
61
+ elif obj_type == "function":
62
+ outline_type = "function"
63
+ else:
64
+ outline_type = obj_type
65
+ docstring = extract_docstring(lines, start, end)
66
+ signature, decorators, signature_lineno = extract_signature_and_decorators(
67
+ lines, start - 1
68
+ )
69
+ outline.append(
70
+ {
71
+ "type": outline_type,
72
+ "name": name,
73
+ "start": start,
74
+ "end": end,
75
+ "parent": parent,
76
+ "signature": signature,
77
+ "decorators": decorators,
78
+ "docstring": docstring,
79
+ }
80
+ )
81
+
82
+
83
+ def parse_python_outline_v2(lines: List[str]):
84
+ class_pat = re.compile(r"^(\s*)class\s+(\w+)")
85
+ func_pat = re.compile(r"^(\s*)def\s+(\w+)")
86
+ assign_pat = re.compile(r"^(\s*)([A-Za-z_][A-Za-z0-9_]*)\s*=.*")
87
+ main_pat = re.compile(r"^\s*if\s+__name__\s*==\s*[\'\"]__main__[\'\"]\s*:")
88
+ outline = []
89
+ stack = []
90
+ obj_ranges = []
91
+ last_top_obj = None
92
+ for idx, line in enumerate(lines):
93
+ class_match = class_pat.match(line)
94
+ func_match = func_pat.match(line)
95
+ assign_match = assign_pat.match(line)
96
+ indent = len(line) - len(line.lstrip())
97
+ parent = ""
98
+ for s in reversed(stack):
99
+ if s[0] == "class" and indent > s[2]:
100
+ parent = s[1]
101
+ break
102
+ if class_match:
103
+ obj = ("class", class_match.group(2), idx + 1, None, parent, indent)
104
+ stack.append(obj)
105
+ last_top_obj = obj
106
+ elif func_match:
107
+ obj = ("function", func_match.group(2), idx + 1, None, parent, indent)
108
+ stack.append(obj)
109
+ last_top_obj = obj
110
+ elif assign_match and indent == 0:
111
+ outline.append(
112
+ {
113
+ "type": "const" if assign_match.group(2).isupper() else "var",
114
+ "name": assign_match.group(2),
115
+ "start": idx + 1,
116
+ "end": idx + 1,
117
+ "parent": "",
118
+ "signature": line.strip(),
119
+ "decorators": [],
120
+ "docstring": "",
121
+ }
122
+ )
123
+ if line.strip().startswith("if __name__ == "):
124
+ outline.append(
125
+ {
126
+ "type": "main",
127
+ "name": "__main__",
128
+ "start": idx + 1,
129
+ "end": idx + 1,
130
+ "parent": "",
131
+ "signature": line.strip(),
132
+ "decorators": [],
133
+ "docstring": "",
134
+ }
135
+ )
136
+ # Close stack objects if indent falls back
137
+ while stack and indent <= stack[-1][5] and idx + 1 > stack[-1][2]:
138
+ finished = stack.pop()
139
+ outline_entry = finished[:2] + (
140
+ finished[2],
141
+ idx + 1,
142
+ finished[4],
143
+ finished[5],
144
+ )
145
+ build_outline_entry(outline_entry, lines, outline)
146
+ # Close any remaining objects
147
+ while stack:
148
+ finished = stack.pop()
149
+ outline_entry = finished[:2] + (
150
+ finished[2],
151
+ len(lines),
152
+ finished[4],
153
+ finished[5],
154
+ )
155
+ build_outline_entry(outline_entry, lines, outline)
156
+ return outline