alita-sdk 0.3.257__py3-none-any.whl → 0.3.562__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.
- alita_sdk/cli/__init__.py +10 -0
- alita_sdk/cli/__main__.py +17 -0
- alita_sdk/cli/agent/__init__.py +5 -0
- alita_sdk/cli/agent/default.py +258 -0
- alita_sdk/cli/agent_executor.py +155 -0
- alita_sdk/cli/agent_loader.py +215 -0
- alita_sdk/cli/agent_ui.py +228 -0
- alita_sdk/cli/agents.py +3601 -0
- alita_sdk/cli/callbacks.py +647 -0
- alita_sdk/cli/cli.py +168 -0
- alita_sdk/cli/config.py +306 -0
- alita_sdk/cli/context/__init__.py +30 -0
- alita_sdk/cli/context/cleanup.py +198 -0
- alita_sdk/cli/context/manager.py +731 -0
- alita_sdk/cli/context/message.py +285 -0
- alita_sdk/cli/context/strategies.py +289 -0
- alita_sdk/cli/context/token_estimation.py +127 -0
- alita_sdk/cli/formatting.py +182 -0
- alita_sdk/cli/input_handler.py +419 -0
- alita_sdk/cli/inventory.py +1073 -0
- alita_sdk/cli/mcp_loader.py +315 -0
- alita_sdk/cli/toolkit.py +327 -0
- alita_sdk/cli/toolkit_loader.py +85 -0
- alita_sdk/cli/tools/__init__.py +43 -0
- alita_sdk/cli/tools/approval.py +224 -0
- alita_sdk/cli/tools/filesystem.py +1751 -0
- alita_sdk/cli/tools/planning.py +389 -0
- alita_sdk/cli/tools/terminal.py +414 -0
- alita_sdk/community/__init__.py +72 -12
- alita_sdk/community/inventory/__init__.py +236 -0
- alita_sdk/community/inventory/config.py +257 -0
- alita_sdk/community/inventory/enrichment.py +2137 -0
- alita_sdk/community/inventory/extractors.py +1469 -0
- alita_sdk/community/inventory/ingestion.py +3172 -0
- alita_sdk/community/inventory/knowledge_graph.py +1457 -0
- alita_sdk/community/inventory/parsers/__init__.py +218 -0
- alita_sdk/community/inventory/parsers/base.py +295 -0
- alita_sdk/community/inventory/parsers/csharp_parser.py +907 -0
- alita_sdk/community/inventory/parsers/go_parser.py +851 -0
- alita_sdk/community/inventory/parsers/html_parser.py +389 -0
- alita_sdk/community/inventory/parsers/java_parser.py +593 -0
- alita_sdk/community/inventory/parsers/javascript_parser.py +629 -0
- alita_sdk/community/inventory/parsers/kotlin_parser.py +768 -0
- alita_sdk/community/inventory/parsers/markdown_parser.py +362 -0
- alita_sdk/community/inventory/parsers/python_parser.py +604 -0
- alita_sdk/community/inventory/parsers/rust_parser.py +858 -0
- alita_sdk/community/inventory/parsers/swift_parser.py +832 -0
- alita_sdk/community/inventory/parsers/text_parser.py +322 -0
- alita_sdk/community/inventory/parsers/yaml_parser.py +370 -0
- alita_sdk/community/inventory/patterns/__init__.py +61 -0
- alita_sdk/community/inventory/patterns/ast_adapter.py +380 -0
- alita_sdk/community/inventory/patterns/loader.py +348 -0
- alita_sdk/community/inventory/patterns/registry.py +198 -0
- alita_sdk/community/inventory/presets.py +535 -0
- alita_sdk/community/inventory/retrieval.py +1403 -0
- alita_sdk/community/inventory/toolkit.py +173 -0
- alita_sdk/community/inventory/toolkit_utils.py +176 -0
- alita_sdk/community/inventory/visualize.py +1370 -0
- alita_sdk/configurations/__init__.py +11 -0
- alita_sdk/configurations/ado.py +148 -2
- alita_sdk/configurations/azure_search.py +1 -1
- alita_sdk/configurations/bigquery.py +1 -1
- alita_sdk/configurations/bitbucket.py +94 -2
- alita_sdk/configurations/browser.py +18 -0
- alita_sdk/configurations/carrier.py +19 -0
- alita_sdk/configurations/confluence.py +130 -1
- alita_sdk/configurations/delta_lake.py +1 -1
- alita_sdk/configurations/figma.py +76 -5
- alita_sdk/configurations/github.py +65 -1
- alita_sdk/configurations/gitlab.py +81 -0
- alita_sdk/configurations/google_places.py +17 -0
- alita_sdk/configurations/jira.py +103 -0
- alita_sdk/configurations/openapi.py +111 -0
- alita_sdk/configurations/postman.py +1 -1
- alita_sdk/configurations/qtest.py +72 -3
- alita_sdk/configurations/report_portal.py +115 -0
- alita_sdk/configurations/salesforce.py +19 -0
- alita_sdk/configurations/service_now.py +1 -12
- alita_sdk/configurations/sharepoint.py +167 -0
- alita_sdk/configurations/sonar.py +18 -0
- alita_sdk/configurations/sql.py +20 -0
- alita_sdk/configurations/testio.py +101 -0
- alita_sdk/configurations/testrail.py +88 -0
- alita_sdk/configurations/xray.py +94 -1
- alita_sdk/configurations/zephyr_enterprise.py +94 -1
- alita_sdk/configurations/zephyr_essential.py +95 -0
- alita_sdk/runtime/clients/artifact.py +21 -4
- alita_sdk/runtime/clients/client.py +458 -67
- alita_sdk/runtime/clients/mcp_discovery.py +342 -0
- alita_sdk/runtime/clients/mcp_manager.py +262 -0
- alita_sdk/runtime/clients/sandbox_client.py +352 -0
- alita_sdk/runtime/langchain/_constants_bkup.py +1318 -0
- alita_sdk/runtime/langchain/assistant.py +183 -43
- alita_sdk/runtime/langchain/constants.py +647 -1
- alita_sdk/runtime/langchain/document_loaders/AlitaDocxMammothLoader.py +315 -3
- alita_sdk/runtime/langchain/document_loaders/AlitaExcelLoader.py +209 -31
- alita_sdk/runtime/langchain/document_loaders/AlitaImageLoader.py +1 -1
- alita_sdk/runtime/langchain/document_loaders/AlitaJSONLinesLoader.py +77 -0
- alita_sdk/runtime/langchain/document_loaders/AlitaJSONLoader.py +10 -3
- alita_sdk/runtime/langchain/document_loaders/AlitaMarkdownLoader.py +66 -0
- alita_sdk/runtime/langchain/document_loaders/AlitaPDFLoader.py +79 -10
- alita_sdk/runtime/langchain/document_loaders/AlitaPowerPointLoader.py +52 -15
- alita_sdk/runtime/langchain/document_loaders/AlitaPythonLoader.py +9 -0
- alita_sdk/runtime/langchain/document_loaders/AlitaTableLoader.py +1 -4
- alita_sdk/runtime/langchain/document_loaders/AlitaTextLoader.py +15 -2
- alita_sdk/runtime/langchain/document_loaders/ImageParser.py +30 -0
- alita_sdk/runtime/langchain/document_loaders/constants.py +189 -41
- alita_sdk/runtime/langchain/interfaces/llm_processor.py +4 -2
- alita_sdk/runtime/langchain/langraph_agent.py +407 -92
- alita_sdk/runtime/langchain/utils.py +102 -8
- alita_sdk/runtime/llms/preloaded.py +2 -6
- alita_sdk/runtime/models/mcp_models.py +61 -0
- alita_sdk/runtime/skills/__init__.py +91 -0
- alita_sdk/runtime/skills/callbacks.py +498 -0
- alita_sdk/runtime/skills/discovery.py +540 -0
- alita_sdk/runtime/skills/executor.py +610 -0
- alita_sdk/runtime/skills/input_builder.py +371 -0
- alita_sdk/runtime/skills/models.py +330 -0
- alita_sdk/runtime/skills/registry.py +355 -0
- alita_sdk/runtime/skills/skill_runner.py +330 -0
- alita_sdk/runtime/toolkits/__init__.py +28 -0
- alita_sdk/runtime/toolkits/application.py +14 -4
- alita_sdk/runtime/toolkits/artifact.py +24 -9
- alita_sdk/runtime/toolkits/datasource.py +13 -6
- alita_sdk/runtime/toolkits/mcp.py +780 -0
- alita_sdk/runtime/toolkits/planning.py +178 -0
- alita_sdk/runtime/toolkits/skill_router.py +238 -0
- alita_sdk/runtime/toolkits/subgraph.py +11 -6
- alita_sdk/runtime/toolkits/tools.py +314 -70
- alita_sdk/runtime/toolkits/vectorstore.py +11 -5
- alita_sdk/runtime/tools/__init__.py +24 -0
- alita_sdk/runtime/tools/application.py +16 -4
- alita_sdk/runtime/tools/artifact.py +367 -33
- alita_sdk/runtime/tools/data_analysis.py +183 -0
- alita_sdk/runtime/tools/function.py +100 -4
- alita_sdk/runtime/tools/graph.py +81 -0
- alita_sdk/runtime/tools/image_generation.py +218 -0
- alita_sdk/runtime/tools/llm.py +1013 -177
- alita_sdk/runtime/tools/loop.py +3 -1
- alita_sdk/runtime/tools/loop_output.py +3 -1
- alita_sdk/runtime/tools/mcp_inspect_tool.py +284 -0
- alita_sdk/runtime/tools/mcp_remote_tool.py +181 -0
- alita_sdk/runtime/tools/mcp_server_tool.py +3 -1
- alita_sdk/runtime/tools/planning/__init__.py +36 -0
- alita_sdk/runtime/tools/planning/models.py +246 -0
- alita_sdk/runtime/tools/planning/wrapper.py +607 -0
- alita_sdk/runtime/tools/router.py +2 -1
- alita_sdk/runtime/tools/sandbox.py +375 -0
- alita_sdk/runtime/tools/skill_router.py +776 -0
- alita_sdk/runtime/tools/tool.py +3 -1
- alita_sdk/runtime/tools/vectorstore.py +69 -65
- alita_sdk/runtime/tools/vectorstore_base.py +163 -90
- alita_sdk/runtime/utils/AlitaCallback.py +137 -21
- alita_sdk/runtime/utils/mcp_client.py +492 -0
- alita_sdk/runtime/utils/mcp_oauth.py +361 -0
- alita_sdk/runtime/utils/mcp_sse_client.py +434 -0
- alita_sdk/runtime/utils/mcp_tools_discovery.py +124 -0
- alita_sdk/runtime/utils/streamlit.py +41 -14
- alita_sdk/runtime/utils/toolkit_utils.py +28 -9
- alita_sdk/runtime/utils/utils.py +48 -0
- alita_sdk/tools/__init__.py +135 -37
- alita_sdk/tools/ado/__init__.py +2 -2
- alita_sdk/tools/ado/repos/__init__.py +15 -19
- alita_sdk/tools/ado/repos/repos_wrapper.py +12 -20
- alita_sdk/tools/ado/test_plan/__init__.py +26 -8
- alita_sdk/tools/ado/test_plan/test_plan_wrapper.py +56 -28
- alita_sdk/tools/ado/wiki/__init__.py +27 -12
- alita_sdk/tools/ado/wiki/ado_wrapper.py +114 -40
- alita_sdk/tools/ado/work_item/__init__.py +27 -12
- alita_sdk/tools/ado/work_item/ado_wrapper.py +95 -11
- alita_sdk/tools/advanced_jira_mining/__init__.py +12 -8
- alita_sdk/tools/aws/delta_lake/__init__.py +14 -11
- alita_sdk/tools/aws/delta_lake/tool.py +5 -1
- alita_sdk/tools/azure_ai/search/__init__.py +13 -8
- alita_sdk/tools/base/tool.py +5 -1
- alita_sdk/tools/base_indexer_toolkit.py +454 -110
- alita_sdk/tools/bitbucket/__init__.py +27 -19
- alita_sdk/tools/bitbucket/api_wrapper.py +285 -27
- alita_sdk/tools/bitbucket/cloud_api_wrapper.py +5 -5
- alita_sdk/tools/browser/__init__.py +41 -16
- alita_sdk/tools/browser/crawler.py +3 -1
- alita_sdk/tools/browser/utils.py +15 -6
- alita_sdk/tools/carrier/__init__.py +18 -17
- alita_sdk/tools/carrier/backend_reports_tool.py +8 -4
- alita_sdk/tools/carrier/excel_reporter.py +8 -4
- alita_sdk/tools/chunkers/__init__.py +3 -1
- alita_sdk/tools/chunkers/code/codeparser.py +1 -1
- alita_sdk/tools/chunkers/sematic/json_chunker.py +2 -1
- alita_sdk/tools/chunkers/sematic/markdown_chunker.py +97 -6
- alita_sdk/tools/chunkers/sematic/proposal_chunker.py +1 -1
- alita_sdk/tools/chunkers/universal_chunker.py +270 -0
- alita_sdk/tools/cloud/aws/__init__.py +11 -7
- alita_sdk/tools/cloud/azure/__init__.py +11 -7
- alita_sdk/tools/cloud/gcp/__init__.py +11 -7
- alita_sdk/tools/cloud/k8s/__init__.py +11 -7
- alita_sdk/tools/code/linter/__init__.py +9 -8
- alita_sdk/tools/code/loaders/codesearcher.py +3 -2
- alita_sdk/tools/code/sonar/__init__.py +20 -13
- alita_sdk/tools/code_indexer_toolkit.py +199 -0
- alita_sdk/tools/confluence/__init__.py +21 -14
- alita_sdk/tools/confluence/api_wrapper.py +197 -58
- alita_sdk/tools/confluence/loader.py +14 -2
- alita_sdk/tools/custom_open_api/__init__.py +11 -5
- alita_sdk/tools/elastic/__init__.py +10 -8
- alita_sdk/tools/elitea_base.py +546 -64
- alita_sdk/tools/figma/__init__.py +11 -8
- alita_sdk/tools/figma/api_wrapper.py +352 -153
- alita_sdk/tools/github/__init__.py +17 -17
- alita_sdk/tools/github/api_wrapper.py +9 -26
- alita_sdk/tools/github/github_client.py +81 -12
- alita_sdk/tools/github/schemas.py +2 -1
- alita_sdk/tools/github/tool.py +5 -1
- alita_sdk/tools/gitlab/__init__.py +18 -13
- alita_sdk/tools/gitlab/api_wrapper.py +224 -80
- alita_sdk/tools/gitlab_org/__init__.py +13 -10
- alita_sdk/tools/google/bigquery/__init__.py +13 -13
- alita_sdk/tools/google/bigquery/tool.py +5 -1
- alita_sdk/tools/google_places/__init__.py +20 -11
- alita_sdk/tools/jira/__init__.py +21 -11
- alita_sdk/tools/jira/api_wrapper.py +315 -168
- alita_sdk/tools/keycloak/__init__.py +10 -8
- alita_sdk/tools/localgit/__init__.py +8 -3
- alita_sdk/tools/localgit/local_git.py +62 -54
- alita_sdk/tools/localgit/tool.py +5 -1
- alita_sdk/tools/memory/__init__.py +38 -14
- alita_sdk/tools/non_code_indexer_toolkit.py +7 -2
- alita_sdk/tools/ocr/__init__.py +10 -8
- alita_sdk/tools/openapi/__init__.py +281 -108
- alita_sdk/tools/openapi/api_wrapper.py +883 -0
- alita_sdk/tools/openapi/tool.py +20 -0
- alita_sdk/tools/pandas/__init__.py +18 -11
- alita_sdk/tools/pandas/api_wrapper.py +40 -45
- alita_sdk/tools/pandas/dataframe/generator/base.py +3 -1
- alita_sdk/tools/postman/__init__.py +10 -11
- alita_sdk/tools/postman/api_wrapper.py +19 -8
- alita_sdk/tools/postman/postman_analysis.py +8 -1
- alita_sdk/tools/pptx/__init__.py +10 -10
- alita_sdk/tools/qtest/__init__.py +21 -14
- alita_sdk/tools/qtest/api_wrapper.py +1784 -88
- alita_sdk/tools/rally/__init__.py +12 -10
- alita_sdk/tools/report_portal/__init__.py +22 -16
- alita_sdk/tools/salesforce/__init__.py +21 -16
- alita_sdk/tools/servicenow/__init__.py +20 -16
- alita_sdk/tools/servicenow/api_wrapper.py +1 -1
- alita_sdk/tools/sharepoint/__init__.py +16 -14
- alita_sdk/tools/sharepoint/api_wrapper.py +179 -39
- alita_sdk/tools/sharepoint/authorization_helper.py +191 -1
- alita_sdk/tools/sharepoint/utils.py +8 -2
- alita_sdk/tools/slack/__init__.py +11 -7
- alita_sdk/tools/sql/__init__.py +21 -19
- alita_sdk/tools/sql/api_wrapper.py +71 -23
- alita_sdk/tools/testio/__init__.py +20 -13
- alita_sdk/tools/testrail/__init__.py +12 -11
- alita_sdk/tools/testrail/api_wrapper.py +214 -46
- alita_sdk/tools/utils/__init__.py +28 -4
- alita_sdk/tools/utils/content_parser.py +182 -62
- alita_sdk/tools/utils/text_operations.py +254 -0
- alita_sdk/tools/vector_adapters/VectorStoreAdapter.py +83 -27
- alita_sdk/tools/xray/__init__.py +17 -14
- alita_sdk/tools/xray/api_wrapper.py +58 -113
- alita_sdk/tools/yagmail/__init__.py +8 -3
- alita_sdk/tools/zephyr/__init__.py +11 -7
- alita_sdk/tools/zephyr_enterprise/__init__.py +15 -9
- alita_sdk/tools/zephyr_enterprise/api_wrapper.py +30 -15
- alita_sdk/tools/zephyr_essential/__init__.py +15 -10
- alita_sdk/tools/zephyr_essential/api_wrapper.py +297 -54
- alita_sdk/tools/zephyr_essential/client.py +6 -4
- alita_sdk/tools/zephyr_scale/__init__.py +12 -8
- alita_sdk/tools/zephyr_scale/api_wrapper.py +39 -31
- alita_sdk/tools/zephyr_squad/__init__.py +11 -7
- {alita_sdk-0.3.257.dist-info → alita_sdk-0.3.562.dist-info}/METADATA +184 -37
- alita_sdk-0.3.562.dist-info/RECORD +450 -0
- alita_sdk-0.3.562.dist-info/entry_points.txt +2 -0
- alita_sdk/tools/bitbucket/tools.py +0 -304
- alita_sdk-0.3.257.dist-info/RECORD +0 -343
- {alita_sdk-0.3.257.dist-info → alita_sdk-0.3.562.dist-info}/WHEEL +0 -0
- {alita_sdk-0.3.257.dist-info → alita_sdk-0.3.562.dist-info}/licenses/LICENSE +0 -0
- {alita_sdk-0.3.257.dist-info → alita_sdk-0.3.562.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,1751 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Filesystem tools for CLI agents.
|
|
3
|
+
|
|
4
|
+
Provides comprehensive file system operations restricted to specific directories.
|
|
5
|
+
Inspired by MCP filesystem server implementation.
|
|
6
|
+
|
|
7
|
+
Also provides a FilesystemApiWrapper for integration with the inventory ingestion
|
|
8
|
+
pipeline, enabling local document loading and chunking.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import base64
|
|
12
|
+
import fnmatch
|
|
13
|
+
import hashlib
|
|
14
|
+
import logging
|
|
15
|
+
import os
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Optional, List, Dict, Any, Generator, ClassVar
|
|
18
|
+
from datetime import datetime
|
|
19
|
+
from langchain_core.tools import BaseTool, ToolException
|
|
20
|
+
from langchain_core.documents import Document
|
|
21
|
+
from pydantic import BaseModel, Field, model_validator
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Maximum recommended content size for single write operations (in characters)
|
|
27
|
+
MAX_RECOMMENDED_CONTENT_SIZE = 5000 # ~5KB, roughly 1,200-1,500 tokens
|
|
28
|
+
|
|
29
|
+
# Helpful error message for truncated content
|
|
30
|
+
CONTENT_TRUNCATED_ERROR = """
|
|
31
|
+
⚠️ CONTENT FIELD MISSING - OUTPUT TRUNCATED
|
|
32
|
+
|
|
33
|
+
Your tool call was cut off because the content was too large for the context window.
|
|
34
|
+
The JSON was truncated, leaving the 'content' field incomplete or missing.
|
|
35
|
+
|
|
36
|
+
🔧 HOW TO FIX THIS:
|
|
37
|
+
|
|
38
|
+
1. **Use incremental writes** - Don't write large files in one call:
|
|
39
|
+
- First: filesystem_write_file(path, "# Header\\nimport x\\n\\n")
|
|
40
|
+
- Then: filesystem_append_file(path, "def func1():\\n ...\\n\\n")
|
|
41
|
+
- Then: filesystem_append_file(path, "def func2():\\n ...\\n\\n")
|
|
42
|
+
|
|
43
|
+
2. **Keep each chunk small** - Under 2000 characters per call
|
|
44
|
+
|
|
45
|
+
3. **Structure first, details later**:
|
|
46
|
+
- Write skeleton/structure first
|
|
47
|
+
- Add implementations section by section
|
|
48
|
+
|
|
49
|
+
4. **For documentation/reports**:
|
|
50
|
+
- Write one section at a time
|
|
51
|
+
- Use append_file for each new section
|
|
52
|
+
|
|
53
|
+
❌ DON'T: Try to write the entire file content again
|
|
54
|
+
✅ DO: Break it into 3-5 smaller append_file calls
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ReadFileInput(BaseModel):
|
|
59
|
+
"""Input for reading a file."""
|
|
60
|
+
path: str = Field(description="Relative path to the file to read")
|
|
61
|
+
head: Optional[int] = Field(None, description="If provided, read only the first N lines")
|
|
62
|
+
tail: Optional[int] = Field(None, description="If provided, read only the last N lines")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ReadFileChunkInput(BaseModel):
|
|
66
|
+
"""Input for reading a file in chunks."""
|
|
67
|
+
path: str = Field(description="Relative path to the file to read")
|
|
68
|
+
start_line: int = Field(default=1, description="Starting line number (1-indexed)")
|
|
69
|
+
end_line: Optional[int] = Field(None, description="Ending line number (inclusive). If None, read to end of file")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class ApplyPatchInput(BaseModel):
|
|
73
|
+
"""Input for applying multiple edits to a file."""
|
|
74
|
+
path: str = Field(description="Relative path to the file to edit")
|
|
75
|
+
edits: List[Dict[str, str]] = Field(
|
|
76
|
+
description="List of edits, each with 'old_text' and 'new_text' keys. Applied sequentially."
|
|
77
|
+
)
|
|
78
|
+
dry_run: bool = Field(default=False, description="If True, preview changes without applying them")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ReadMultipleFilesInput(BaseModel):
|
|
82
|
+
"""Input for reading multiple files."""
|
|
83
|
+
paths: List[str] = Field(
|
|
84
|
+
min_length=1,
|
|
85
|
+
description="Array of file paths to read. Each path must point to a valid file within allowed directories."
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class WriteFileInput(BaseModel):
|
|
90
|
+
"""Input for writing to a file."""
|
|
91
|
+
path: str = Field(description="Relative path to the file to write")
|
|
92
|
+
content: Optional[str] = Field(
|
|
93
|
+
default=None,
|
|
94
|
+
description="Content to write to the file. REQUIRED - this field cannot be empty or omitted."
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
@model_validator(mode='after')
|
|
98
|
+
def validate_content_required(self):
|
|
99
|
+
"""Provide helpful error message when content is missing or truncated."""
|
|
100
|
+
if self.content is None:
|
|
101
|
+
raise ToolException(CONTENT_TRUNCATED_ERROR)
|
|
102
|
+
if len(self.content) > MAX_RECOMMENDED_CONTENT_SIZE:
|
|
103
|
+
logger.warning(
|
|
104
|
+
f"Content is very large ({len(self.content)} chars). Consider using append_file "
|
|
105
|
+
"for incremental writes to avoid truncation issues."
|
|
106
|
+
)
|
|
107
|
+
return self
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class AppendFileInput(BaseModel):
|
|
111
|
+
"""Input for appending to a file."""
|
|
112
|
+
path: str = Field(description="Relative path to the file to append to")
|
|
113
|
+
content: Optional[str] = Field(
|
|
114
|
+
default=None,
|
|
115
|
+
description="Content to append to the end of the file. REQUIRED - this field cannot be empty or omitted."
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
@model_validator(mode='after')
|
|
119
|
+
def validate_content_required(self):
|
|
120
|
+
"""Provide helpful error message when content is missing or truncated."""
|
|
121
|
+
if self.content is None:
|
|
122
|
+
raise ToolException(CONTENT_TRUNCATED_ERROR)
|
|
123
|
+
return self
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class EditFileInput(BaseModel):
|
|
127
|
+
"""Input for editing a file with precise replacements."""
|
|
128
|
+
path: str = Field(description="Relative path to the file to edit")
|
|
129
|
+
old_text: str = Field(description="Exact text to search for and replace")
|
|
130
|
+
new_text: str = Field(description="Text to replace with")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class ListDirectoryInput(BaseModel):
|
|
134
|
+
"""Input for listing directory contents."""
|
|
135
|
+
path: str = Field(default=".", description="Relative path to the directory to list")
|
|
136
|
+
include_sizes: bool = Field(default=False, description="Include file sizes in the output")
|
|
137
|
+
sort_by: str = Field(default="name", description="Sort by 'name' or 'size'")
|
|
138
|
+
max_results: Optional[int] = Field(default=200, description="Maximum number of entries to return. Default is 200 to prevent context overflow.")
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class DirectoryTreeInput(BaseModel):
|
|
142
|
+
"""Input for getting a directory tree."""
|
|
143
|
+
path: str = Field(default=".", description="Relative path to the directory")
|
|
144
|
+
max_depth: Optional[int] = Field(default=3, description="Maximum depth to traverse. Default is 3 to prevent excessive output. Use None for unlimited (caution: may exceed context limits).")
|
|
145
|
+
max_items: Optional[int] = Field(default=200, description="Maximum number of files/directories to include. Default is 200 to prevent context window overflow. Use None for unlimited (caution: large directories may exceed context limits).")
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class SearchFilesInput(BaseModel):
|
|
149
|
+
"""Input for searching files."""
|
|
150
|
+
path: str = Field(default=".", description="Relative path to search from")
|
|
151
|
+
pattern: str = Field(description="Glob pattern to match (e.g., '*.py', '**/*.txt')")
|
|
152
|
+
max_results: Optional[int] = Field(default=100, description="Maximum number of results to return. Default is 100 to prevent context overflow. Use None for unlimited.")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class DeleteFileInput(BaseModel):
|
|
156
|
+
"""Input for deleting a file."""
|
|
157
|
+
path: str = Field(description="Relative path to the file to delete")
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class MoveFileInput(BaseModel):
|
|
161
|
+
"""Input for moving/renaming a file."""
|
|
162
|
+
source: str = Field(description="Relative path to the source file")
|
|
163
|
+
destination: str = Field(description="Relative path to the destination")
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class CreateDirectoryInput(BaseModel):
|
|
167
|
+
"""Input for creating a directory."""
|
|
168
|
+
path: str = Field(description="Relative path to the directory to create")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class GetFileInfoInput(BaseModel):
|
|
172
|
+
"""Input for getting file information."""
|
|
173
|
+
path: str = Field(description="Relative path to the file or directory")
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class EmptyInput(BaseModel):
|
|
177
|
+
"""Empty input schema for tools that take no arguments."""
|
|
178
|
+
pass
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class FileSystemTool(BaseTool):
|
|
182
|
+
"""Base class for filesystem tools with directory restriction."""
|
|
183
|
+
base_directory: str # Primary directory (for backward compatibility)
|
|
184
|
+
allowed_directories: List[str] = [] # Additional allowed directories
|
|
185
|
+
_basename_collision_detected: bool = False # Cache for collision detection
|
|
186
|
+
_basename_collision_checked: bool = False # Whether we've checked for collisions
|
|
187
|
+
|
|
188
|
+
def _get_all_allowed_directories(self) -> List[Path]:
|
|
189
|
+
"""Get all allowed directories as resolved Paths."""
|
|
190
|
+
dirs = [Path(self.base_directory).resolve()]
|
|
191
|
+
for d in self.allowed_directories:
|
|
192
|
+
resolved = Path(d).resolve()
|
|
193
|
+
if resolved not in dirs:
|
|
194
|
+
dirs.append(resolved)
|
|
195
|
+
return dirs
|
|
196
|
+
|
|
197
|
+
def _check_basename_collision(self) -> bool:
|
|
198
|
+
"""Check if multiple allowed directories have the same basename."""
|
|
199
|
+
if self._basename_collision_checked:
|
|
200
|
+
return self._basename_collision_detected
|
|
201
|
+
|
|
202
|
+
allowed_dirs = self._get_all_allowed_directories()
|
|
203
|
+
basenames = [d.name for d in allowed_dirs]
|
|
204
|
+
self._basename_collision_detected = len(basenames) != len(set(basenames))
|
|
205
|
+
self._basename_collision_checked = True
|
|
206
|
+
return self._basename_collision_detected
|
|
207
|
+
|
|
208
|
+
def _get_relative_path_from_allowed_dirs(self, absolute_path: Path) -> tuple:
|
|
209
|
+
"""Get relative path and directory name for a file in allowed directories.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
absolute_path: Absolute path to the file
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
Tuple of (relative_path, directory_name)
|
|
216
|
+
|
|
217
|
+
Raises:
|
|
218
|
+
ValueError: If path is not within any allowed directory
|
|
219
|
+
"""
|
|
220
|
+
allowed_dirs = self._get_all_allowed_directories()
|
|
221
|
+
|
|
222
|
+
# Find which allowed directory contains this path
|
|
223
|
+
for base in allowed_dirs:
|
|
224
|
+
try:
|
|
225
|
+
rel_path = absolute_path.relative_to(base)
|
|
226
|
+
|
|
227
|
+
# Determine directory name for prefix
|
|
228
|
+
if self._check_basename_collision():
|
|
229
|
+
# Use parent/basename format to disambiguate
|
|
230
|
+
dir_name = f"{base.parent.name}/{base.name}"
|
|
231
|
+
else:
|
|
232
|
+
# Use just basename
|
|
233
|
+
dir_name = base.name
|
|
234
|
+
|
|
235
|
+
return (str(rel_path), dir_name)
|
|
236
|
+
except ValueError:
|
|
237
|
+
continue
|
|
238
|
+
|
|
239
|
+
# Path not in any allowed directory
|
|
240
|
+
allowed_paths = [str(d) for d in allowed_dirs]
|
|
241
|
+
raise ValueError(
|
|
242
|
+
f"Path '{absolute_path}' is not within any allowed directory.\n"
|
|
243
|
+
f"Allowed directories: {allowed_paths}\n"
|
|
244
|
+
f"Attempted path: {absolute_path}"
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
def _resolve_path(self, relative_path: str) -> Path:
|
|
248
|
+
"""
|
|
249
|
+
Resolve and validate a path within any of the allowed directories.
|
|
250
|
+
|
|
251
|
+
Security: Ensures resolved path is within one of the allowed directories.
|
|
252
|
+
"""
|
|
253
|
+
allowed_dirs = self._get_all_allowed_directories()
|
|
254
|
+
|
|
255
|
+
# Handle absolute paths - check if within any allowed directory
|
|
256
|
+
if Path(relative_path).is_absolute():
|
|
257
|
+
target = Path(relative_path).resolve()
|
|
258
|
+
for base in allowed_dirs:
|
|
259
|
+
try:
|
|
260
|
+
target.relative_to(base)
|
|
261
|
+
return target
|
|
262
|
+
except ValueError:
|
|
263
|
+
continue
|
|
264
|
+
raise ValueError(f"Access denied: path '{relative_path}' is outside allowed directories")
|
|
265
|
+
|
|
266
|
+
# For relative paths, try to resolve against each allowed directory
|
|
267
|
+
# First check primary base_directory
|
|
268
|
+
primary_base = allowed_dirs[0]
|
|
269
|
+
target = (primary_base / relative_path).resolve()
|
|
270
|
+
|
|
271
|
+
# Check if target is within any allowed directory
|
|
272
|
+
for base in allowed_dirs:
|
|
273
|
+
try:
|
|
274
|
+
target.relative_to(base)
|
|
275
|
+
return target
|
|
276
|
+
except ValueError:
|
|
277
|
+
continue
|
|
278
|
+
|
|
279
|
+
# If relative path doesn't work from primary, try finding the file in other directories
|
|
280
|
+
for base in allowed_dirs[1:]:
|
|
281
|
+
candidate = (base / relative_path).resolve()
|
|
282
|
+
if candidate.exists():
|
|
283
|
+
return candidate
|
|
284
|
+
|
|
285
|
+
# Default to primary base directory resolution
|
|
286
|
+
raise ValueError(f"Access denied: path '{relative_path}' is outside allowed directories")
|
|
287
|
+
|
|
288
|
+
def _format_size(self, size: int) -> str:
|
|
289
|
+
"""Format file size in human-readable format."""
|
|
290
|
+
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
|
291
|
+
if size < 1024.0:
|
|
292
|
+
return f"{size:.1f} {unit}"
|
|
293
|
+
size /= 1024.0
|
|
294
|
+
return f"{size:.1f} PB"
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
class ReadFileTool(FileSystemTool):
|
|
298
|
+
"""Read file contents with optional head/tail support."""
|
|
299
|
+
name: str = "filesystem_read_file"
|
|
300
|
+
description: str = (
|
|
301
|
+
"Read the complete contents of a file from the file system. "
|
|
302
|
+
"Handles various text encodings and provides detailed error messages if the file cannot be read. "
|
|
303
|
+
"Use 'head' parameter to read only the first N lines, or 'tail' parameter to read only the last N lines. "
|
|
304
|
+
"Only works within allowed directories."
|
|
305
|
+
)
|
|
306
|
+
args_schema: type[BaseModel] = ReadFileInput
|
|
307
|
+
truncation_suggestions: ClassVar[List[str]] = [
|
|
308
|
+
"Use head=100 to read only the first 100 lines",
|
|
309
|
+
"Use tail=100 to read only the last 100 lines",
|
|
310
|
+
"Use filesystem_read_file_chunk with start_line and end_line for specific sections",
|
|
311
|
+
]
|
|
312
|
+
|
|
313
|
+
def _run(self, path: str, head: Optional[int] = None, tail: Optional[int] = None) -> str:
|
|
314
|
+
"""Read a file with optional head/tail."""
|
|
315
|
+
try:
|
|
316
|
+
target = self._resolve_path(path)
|
|
317
|
+
|
|
318
|
+
if not target.exists():
|
|
319
|
+
return f"Error: File '{path}' does not exist"
|
|
320
|
+
|
|
321
|
+
if not target.is_file():
|
|
322
|
+
return f"Error: '{path}' is not a file"
|
|
323
|
+
|
|
324
|
+
if head and tail:
|
|
325
|
+
return "Error: Cannot specify both head and tail parameters simultaneously"
|
|
326
|
+
|
|
327
|
+
with open(target, 'r', encoding='utf-8') as f:
|
|
328
|
+
if tail:
|
|
329
|
+
lines = f.readlines()
|
|
330
|
+
content = ''.join(lines[-tail:])
|
|
331
|
+
elif head:
|
|
332
|
+
lines = []
|
|
333
|
+
for i, line in enumerate(f):
|
|
334
|
+
if i >= head:
|
|
335
|
+
break
|
|
336
|
+
lines.append(line)
|
|
337
|
+
content = ''.join(lines)
|
|
338
|
+
else:
|
|
339
|
+
content = f.read()
|
|
340
|
+
|
|
341
|
+
char_count = len(content)
|
|
342
|
+
line_count = content.count('\n') + (1 if content and not content.endswith('\n') else 0)
|
|
343
|
+
|
|
344
|
+
return f"Successfully read '{path}' ({char_count} characters, {line_count} lines):\n\n{content}"
|
|
345
|
+
except UnicodeDecodeError:
|
|
346
|
+
return f"Error: File '{path}' appears to be binary or uses an unsupported encoding"
|
|
347
|
+
except Exception as e:
|
|
348
|
+
return f"Error reading file '{path}': {str(e)}"
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class ReadFileChunkTool(FileSystemTool):
|
|
352
|
+
"""Read a file in chunks by line range."""
|
|
353
|
+
name: str = "filesystem_read_file_chunk"
|
|
354
|
+
description: str = (
|
|
355
|
+
"Read a specific range of lines from a file. This is efficient for large files where you only need a portion. "
|
|
356
|
+
"Specify start_line (1-indexed) and optionally end_line. If end_line is omitted, reads to end of file. "
|
|
357
|
+
"Use this to avoid loading entire large files into memory. "
|
|
358
|
+
"Only works within allowed directories."
|
|
359
|
+
)
|
|
360
|
+
args_schema: type[BaseModel] = ReadFileChunkInput
|
|
361
|
+
truncation_suggestions: ClassVar[List[str]] = [
|
|
362
|
+
"Reduce the line range (end_line - start_line) to read fewer lines at once",
|
|
363
|
+
"Read smaller chunks sequentially if you need to process the entire file",
|
|
364
|
+
]
|
|
365
|
+
|
|
366
|
+
def _run(self, path: str, start_line: int = 1, end_line: Optional[int] = None) -> str:
|
|
367
|
+
"""Read a chunk of a file by line range."""
|
|
368
|
+
try:
|
|
369
|
+
target = self._resolve_path(path)
|
|
370
|
+
|
|
371
|
+
if not target.exists():
|
|
372
|
+
return f"Error: File '{path}' does not exist"
|
|
373
|
+
|
|
374
|
+
if not target.is_file():
|
|
375
|
+
return f"Error: '{path}' is not a file"
|
|
376
|
+
|
|
377
|
+
if start_line < 1:
|
|
378
|
+
return "Error: start_line must be >= 1"
|
|
379
|
+
|
|
380
|
+
if end_line is not None and end_line < start_line:
|
|
381
|
+
return "Error: end_line must be >= start_line"
|
|
382
|
+
|
|
383
|
+
lines = []
|
|
384
|
+
with open(target, 'r', encoding='utf-8') as f:
|
|
385
|
+
for i, line in enumerate(f, 1):
|
|
386
|
+
if i < start_line:
|
|
387
|
+
continue
|
|
388
|
+
if end_line is not None and i > end_line:
|
|
389
|
+
break
|
|
390
|
+
lines.append(line)
|
|
391
|
+
|
|
392
|
+
content = ''.join(lines)
|
|
393
|
+
actual_end = end_line if end_line else start_line + len(lines) - 1
|
|
394
|
+
|
|
395
|
+
if not lines:
|
|
396
|
+
return f"No lines found in range {start_line}-{actual_end} in '{path}'"
|
|
397
|
+
|
|
398
|
+
return f"Successfully read '{path}' lines {start_line}-{actual_end} ({len(content)} characters, {len(lines)} lines):\n\n{content}"
|
|
399
|
+
except UnicodeDecodeError:
|
|
400
|
+
return f"Error: File '{path}' appears to be binary or uses an unsupported encoding"
|
|
401
|
+
except Exception as e:
|
|
402
|
+
return f"Error reading file '{path}': {str(e)}"
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
class ReadMultipleFilesTool(FileSystemTool):
|
|
406
|
+
"""Read multiple files simultaneously."""
|
|
407
|
+
name: str = "filesystem_read_multiple_files"
|
|
408
|
+
description: str = (
|
|
409
|
+
"Read the contents of multiple files simultaneously. This is more efficient than reading files one by one. "
|
|
410
|
+
"Each file's content is returned with its path as a reference. "
|
|
411
|
+
"Failed reads for individual files won't stop the entire operation. "
|
|
412
|
+
"Only works within allowed directories."
|
|
413
|
+
)
|
|
414
|
+
args_schema: type[BaseModel] = ReadMultipleFilesInput
|
|
415
|
+
truncation_suggestions: ClassVar[List[str]] = [
|
|
416
|
+
"Read fewer files at once - split into multiple smaller batches",
|
|
417
|
+
"Use filesystem_read_file with head parameter on individual large files instead",
|
|
418
|
+
]
|
|
419
|
+
|
|
420
|
+
def _run(self, paths: List[str]) -> str:
|
|
421
|
+
"""Read multiple files."""
|
|
422
|
+
results = []
|
|
423
|
+
|
|
424
|
+
for file_path in paths:
|
|
425
|
+
try:
|
|
426
|
+
target = self._resolve_path(file_path)
|
|
427
|
+
with open(target, 'r', encoding='utf-8') as f:
|
|
428
|
+
content = f.read()
|
|
429
|
+
results.append(f"{file_path}:\n{content}")
|
|
430
|
+
except Exception as e:
|
|
431
|
+
results.append(f"{file_path}: Error - {str(e)}")
|
|
432
|
+
|
|
433
|
+
return "\n\n---\n\n".join(results)
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
class WriteFileTool(FileSystemTool):
|
|
437
|
+
"""Write content to a file."""
|
|
438
|
+
name: str = "filesystem_write_file"
|
|
439
|
+
description: str = (
|
|
440
|
+
"Create a new file or completely overwrite an existing file with new content. "
|
|
441
|
+
"Use with caution as it will overwrite existing files without warning. "
|
|
442
|
+
"Handles text content with proper encoding. Creates parent directories if needed. "
|
|
443
|
+
"Only works within allowed directories."
|
|
444
|
+
)
|
|
445
|
+
args_schema: type[BaseModel] = WriteFileInput
|
|
446
|
+
|
|
447
|
+
def _run(self, path: str, content: str) -> str:
|
|
448
|
+
"""Write to a file."""
|
|
449
|
+
try:
|
|
450
|
+
target = self._resolve_path(path)
|
|
451
|
+
|
|
452
|
+
# Create parent directories if they don't exist
|
|
453
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
454
|
+
|
|
455
|
+
with open(target, 'w', encoding='utf-8') as f:
|
|
456
|
+
f.write(content)
|
|
457
|
+
|
|
458
|
+
size = len(content.encode('utf-8'))
|
|
459
|
+
return f"Successfully wrote to '{path}' ({self._format_size(size)})"
|
|
460
|
+
except Exception as e:
|
|
461
|
+
return f"Error writing to file '{path}': {str(e)}"
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
class AppendFileTool(FileSystemTool):
|
|
465
|
+
"""Append content to the end of a file."""
|
|
466
|
+
name: str = "filesystem_append_file"
|
|
467
|
+
description: str = (
|
|
468
|
+
"Append content to the end of an existing file. Creates the file if it doesn't exist. "
|
|
469
|
+
"Use this for incremental file creation - write initial structure with write_file, "
|
|
470
|
+
"then add sections progressively with append_file. This is safer than rewriting "
|
|
471
|
+
"entire files and prevents context overflow. Only works within allowed directories."
|
|
472
|
+
)
|
|
473
|
+
args_schema: type[BaseModel] = AppendFileInput
|
|
474
|
+
|
|
475
|
+
def _run(self, path: str, content: str) -> str:
|
|
476
|
+
"""Append to a file."""
|
|
477
|
+
try:
|
|
478
|
+
target = self._resolve_path(path)
|
|
479
|
+
|
|
480
|
+
# Create parent directories if they don't exist
|
|
481
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
482
|
+
|
|
483
|
+
# Check current file size if it exists
|
|
484
|
+
existed = target.exists()
|
|
485
|
+
original_size = target.stat().st_size if existed else 0
|
|
486
|
+
|
|
487
|
+
with open(target, 'a', encoding='utf-8') as f:
|
|
488
|
+
f.write(content)
|
|
489
|
+
|
|
490
|
+
appended_size = len(content.encode('utf-8'))
|
|
491
|
+
new_size = original_size + appended_size
|
|
492
|
+
|
|
493
|
+
if existed:
|
|
494
|
+
return f"Successfully appended {self._format_size(appended_size)} to '{path}' (total: {self._format_size(new_size)})"
|
|
495
|
+
else:
|
|
496
|
+
return f"Created '{path}' and wrote {self._format_size(appended_size)}"
|
|
497
|
+
except Exception as e:
|
|
498
|
+
return f"Error appending to file '{path}': {str(e)}"
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
class EditFileTool(FileSystemTool):
|
|
502
|
+
"""Edit file with precise text replacement."""
|
|
503
|
+
name: str = "filesystem_edit_file"
|
|
504
|
+
description: str = (
|
|
505
|
+
"Make precise edits to a text file by replacing exact text matches. "
|
|
506
|
+
"The old_text must match exactly (including whitespace and line breaks). "
|
|
507
|
+
"This is safer than rewriting entire files when making small changes. "
|
|
508
|
+
"Only works within allowed directories."
|
|
509
|
+
)
|
|
510
|
+
args_schema: type[BaseModel] = EditFileInput
|
|
511
|
+
|
|
512
|
+
def _run(self, path: str, old_text: str, new_text: str) -> str:
|
|
513
|
+
"""Edit a file by replacing exact text."""
|
|
514
|
+
try:
|
|
515
|
+
target = self._resolve_path(path)
|
|
516
|
+
|
|
517
|
+
if not target.exists():
|
|
518
|
+
return f"Error: File '{path}' does not exist"
|
|
519
|
+
|
|
520
|
+
if not target.is_file():
|
|
521
|
+
return f"Error: '{path}' is not a file"
|
|
522
|
+
|
|
523
|
+
# Read current content
|
|
524
|
+
with open(target, 'r', encoding='utf-8') as f:
|
|
525
|
+
content = f.read()
|
|
526
|
+
|
|
527
|
+
# Check if old_text exists
|
|
528
|
+
if old_text not in content:
|
|
529
|
+
return f"Error: Could not find the specified text in '{path}'"
|
|
530
|
+
|
|
531
|
+
# Count occurrences
|
|
532
|
+
occurrences = content.count(old_text)
|
|
533
|
+
if occurrences > 1:
|
|
534
|
+
return f"Error: Found {occurrences} occurrences of the text. Please be more specific to ensure correct replacement."
|
|
535
|
+
|
|
536
|
+
# Replace text
|
|
537
|
+
new_content = content.replace(old_text, new_text)
|
|
538
|
+
|
|
539
|
+
# Write back
|
|
540
|
+
with open(target, 'w', encoding='utf-8') as f:
|
|
541
|
+
f.write(new_content)
|
|
542
|
+
|
|
543
|
+
chars_before = len(old_text)
|
|
544
|
+
chars_after = len(new_text)
|
|
545
|
+
diff = chars_after - chars_before
|
|
546
|
+
|
|
547
|
+
return f"Successfully edited '{path}': replaced {chars_before} characters with {chars_after} characters ({diff:+d} character difference)"
|
|
548
|
+
except Exception as e:
|
|
549
|
+
return f"Error editing file '{path}': {str(e)}"
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
class ApplyPatchTool(FileSystemTool):
|
|
553
|
+
"""Apply multiple edits to a file like a patch."""
|
|
554
|
+
name: str = "filesystem_apply_patch"
|
|
555
|
+
description: str = (
|
|
556
|
+
"Apply multiple precise edits to a file in a single operation, similar to applying a patch. "
|
|
557
|
+
"Each edit specifies 'old_text' (exact text to find) and 'new_text' (replacement text). "
|
|
558
|
+
"Edits are applied sequentially. Use dry_run=true to preview changes without applying them. "
|
|
559
|
+
"This is efficient for making multiple changes to large files. "
|
|
560
|
+
"Only works within allowed directories."
|
|
561
|
+
)
|
|
562
|
+
args_schema: type[BaseModel] = ApplyPatchInput
|
|
563
|
+
|
|
564
|
+
def _run(self, path: str, edits: List[Dict[str, str]], dry_run: bool = False) -> str:
|
|
565
|
+
"""Apply multiple edits to a file."""
|
|
566
|
+
try:
|
|
567
|
+
target = self._resolve_path(path)
|
|
568
|
+
|
|
569
|
+
if not target.exists():
|
|
570
|
+
return f"Error: File '{path}' does not exist"
|
|
571
|
+
|
|
572
|
+
if not target.is_file():
|
|
573
|
+
return f"Error: '{path}' is not a file"
|
|
574
|
+
|
|
575
|
+
# Read current content
|
|
576
|
+
with open(target, 'r', encoding='utf-8') as f:
|
|
577
|
+
original_content = f.read()
|
|
578
|
+
|
|
579
|
+
content = original_content
|
|
580
|
+
changes = []
|
|
581
|
+
|
|
582
|
+
# Apply edits sequentially
|
|
583
|
+
for i, edit in enumerate(edits, 1):
|
|
584
|
+
old_text = edit.get('old_text', '')
|
|
585
|
+
new_text = edit.get('new_text', '')
|
|
586
|
+
|
|
587
|
+
if not old_text:
|
|
588
|
+
return f"Error: Edit #{i} is missing 'old_text'"
|
|
589
|
+
|
|
590
|
+
if old_text not in content:
|
|
591
|
+
return f"Error: Edit #{i} - could not find the specified text in current content"
|
|
592
|
+
|
|
593
|
+
# Count occurrences
|
|
594
|
+
occurrences = content.count(old_text)
|
|
595
|
+
if occurrences > 1:
|
|
596
|
+
return f"Error: Edit #{i} - found {occurrences} occurrences. Please be more specific."
|
|
597
|
+
|
|
598
|
+
# Apply the edit
|
|
599
|
+
content = content.replace(old_text, new_text)
|
|
600
|
+
changes.append({
|
|
601
|
+
'edit_num': i,
|
|
602
|
+
'old_len': len(old_text),
|
|
603
|
+
'new_len': len(new_text),
|
|
604
|
+
'diff': len(new_text) - len(old_text)
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
if dry_run:
|
|
608
|
+
# Show preview in diff-like format
|
|
609
|
+
lines = [f"Preview of changes to '{path}' ({len(edits)} edits):\n"]
|
|
610
|
+
for change in changes:
|
|
611
|
+
lines.append(
|
|
612
|
+
f"Edit #{change['edit_num']}: "
|
|
613
|
+
f"{change['old_len']} → {change['new_len']} chars "
|
|
614
|
+
f"({change['diff']:+d})"
|
|
615
|
+
)
|
|
616
|
+
|
|
617
|
+
total_diff = sum(c['diff'] for c in changes)
|
|
618
|
+
lines.append(f"\nTotal change: {len(original_content)} → {len(content)} chars ({total_diff:+d})")
|
|
619
|
+
lines.append("\n[DRY RUN - No changes written to file]")
|
|
620
|
+
return "\n".join(lines)
|
|
621
|
+
|
|
622
|
+
# Write the modified content
|
|
623
|
+
with open(target, 'w', encoding='utf-8') as f:
|
|
624
|
+
f.write(content)
|
|
625
|
+
|
|
626
|
+
# Build success message
|
|
627
|
+
lines = [f"Successfully applied {len(edits)} edits to '{path}':\n"]
|
|
628
|
+
for change in changes:
|
|
629
|
+
lines.append(
|
|
630
|
+
f"Edit #{change['edit_num']}: "
|
|
631
|
+
f"{change['old_len']} → {change['new_len']} chars "
|
|
632
|
+
f"({change['diff']:+d})"
|
|
633
|
+
)
|
|
634
|
+
|
|
635
|
+
total_diff = sum(c['diff'] for c in changes)
|
|
636
|
+
lines.append(f"\nTotal change: {len(original_content)} → {len(content)} chars ({total_diff:+d})")
|
|
637
|
+
|
|
638
|
+
return "\n".join(lines)
|
|
639
|
+
except Exception as e:
|
|
640
|
+
return f"Error applying patch to '{path}': {str(e)}"
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
class ListDirectoryTool(FileSystemTool):
|
|
644
|
+
"""List directory contents."""
|
|
645
|
+
name: str = "filesystem_list_directory"
|
|
646
|
+
description: str = (
|
|
647
|
+
"Get a detailed listing of all files and directories in a specified path. "
|
|
648
|
+
"Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. "
|
|
649
|
+
"Can optionally include file sizes and sort by name or size. "
|
|
650
|
+
"Only works within allowed directories."
|
|
651
|
+
)
|
|
652
|
+
args_schema: type[BaseModel] = ListDirectoryInput
|
|
653
|
+
truncation_suggestions: ClassVar[List[str]] = [
|
|
654
|
+
"List a specific subdirectory instead of the root directory",
|
|
655
|
+
"Consider using filesystem_directory_tree with max_depth=1 for hierarchical overview",
|
|
656
|
+
]
|
|
657
|
+
|
|
658
|
+
def _run(self, path: str = ".", include_sizes: bool = False, sort_by: str = "name", max_results: Optional[int] = 200) -> str:
|
|
659
|
+
"""List directory contents."""
|
|
660
|
+
try:
|
|
661
|
+
target = self._resolve_path(path)
|
|
662
|
+
|
|
663
|
+
if not target.exists():
|
|
664
|
+
return f"Error: Directory '{path}' does not exist"
|
|
665
|
+
|
|
666
|
+
if not target.is_dir():
|
|
667
|
+
return f"Error: '{path}' is not a directory"
|
|
668
|
+
|
|
669
|
+
entries = []
|
|
670
|
+
for entry in target.iterdir():
|
|
671
|
+
entry_info = {
|
|
672
|
+
'name': entry.name,
|
|
673
|
+
'is_dir': entry.is_dir(),
|
|
674
|
+
'size': entry.stat().st_size if entry.is_file() else 0,
|
|
675
|
+
'path': entry
|
|
676
|
+
}
|
|
677
|
+
entries.append(entry_info)
|
|
678
|
+
|
|
679
|
+
# Sort entries
|
|
680
|
+
if sort_by == "size":
|
|
681
|
+
entries.sort(key=lambda x: x['size'], reverse=True)
|
|
682
|
+
else:
|
|
683
|
+
entries.sort(key=lambda x: x['name'].lower())
|
|
684
|
+
|
|
685
|
+
# Apply limit
|
|
686
|
+
total_count = len(entries)
|
|
687
|
+
truncated = False
|
|
688
|
+
if max_results is not None and total_count > max_results:
|
|
689
|
+
entries = entries[:max_results]
|
|
690
|
+
truncated = True
|
|
691
|
+
|
|
692
|
+
# Get directory name for multi-directory configs
|
|
693
|
+
allowed_dirs = self._get_all_allowed_directories()
|
|
694
|
+
has_multiple_dirs = len(allowed_dirs) > 1
|
|
695
|
+
_, dir_name = self._get_relative_path_from_allowed_dirs(target) if has_multiple_dirs else ("", "")
|
|
696
|
+
|
|
697
|
+
# Format output
|
|
698
|
+
lines = []
|
|
699
|
+
total_files = 0
|
|
700
|
+
total_dirs = 0
|
|
701
|
+
total_size = 0
|
|
702
|
+
|
|
703
|
+
for entry in entries:
|
|
704
|
+
prefix = "[DIR] " if entry['is_dir'] else "[FILE]"
|
|
705
|
+
|
|
706
|
+
# Add directory prefix for multi-directory configs
|
|
707
|
+
if has_multiple_dirs:
|
|
708
|
+
name = f"{dir_name}/{entry['name']}"
|
|
709
|
+
else:
|
|
710
|
+
name = entry['name']
|
|
711
|
+
|
|
712
|
+
if include_sizes and not entry['is_dir']:
|
|
713
|
+
size_str = self._format_size(entry['size'])
|
|
714
|
+
lines.append(f"{prefix} {name:<40} {size_str:>10}")
|
|
715
|
+
total_size += entry['size']
|
|
716
|
+
else:
|
|
717
|
+
lines.append(f"{prefix} {name}")
|
|
718
|
+
|
|
719
|
+
if entry['is_dir']:
|
|
720
|
+
total_dirs += 1
|
|
721
|
+
else:
|
|
722
|
+
total_files += 1
|
|
723
|
+
|
|
724
|
+
result = "\n".join(lines)
|
|
725
|
+
|
|
726
|
+
# Add header showing the listing context
|
|
727
|
+
if path in (".", "", "./"):
|
|
728
|
+
header = "Contents of working directory (./):\n\n"
|
|
729
|
+
else:
|
|
730
|
+
header = f"Contents of {path}/:\n\n"
|
|
731
|
+
result = header + result
|
|
732
|
+
|
|
733
|
+
if include_sizes:
|
|
734
|
+
summary = f"\n\nTotal: {total_files} files, {total_dirs} directories"
|
|
735
|
+
if total_files > 0:
|
|
736
|
+
summary += f"\nCombined size: {self._format_size(total_size)}"
|
|
737
|
+
result += summary
|
|
738
|
+
|
|
739
|
+
if truncated:
|
|
740
|
+
result += f"\n\n⚠️ OUTPUT TRUNCATED: Showing {len(entries)} of {total_count} entries from '{dir_name if has_multiple_dirs else path}' (max_results={max_results})"
|
|
741
|
+
result += "\n To see more: increase max_results or list a specific subdirectory"
|
|
742
|
+
|
|
743
|
+
# Add note about how to access files
|
|
744
|
+
result += "\n\nNote: Access files using paths shown above (e.g., 'agents/file.md' for items in agents/ directory)"
|
|
745
|
+
|
|
746
|
+
return result if lines else "Directory is empty"
|
|
747
|
+
except Exception as e:
|
|
748
|
+
return f"Error listing directory '{path}': {str(e)}"
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
class DirectoryTreeTool(FileSystemTool):
|
|
752
|
+
"""Get recursive directory tree."""
|
|
753
|
+
name: str = "filesystem_directory_tree"
|
|
754
|
+
description: str = (
|
|
755
|
+
"Get a recursive tree view of files and directories. "
|
|
756
|
+
"Shows the complete structure in an easy-to-read tree format. "
|
|
757
|
+
"IMPORTANT: For large directories, use max_depth (default: 3) and max_items (default: 200) "
|
|
758
|
+
"to prevent context window overflow. Increase these only if needed for smaller directories. "
|
|
759
|
+
"Only works within allowed directories."
|
|
760
|
+
)
|
|
761
|
+
args_schema: type[BaseModel] = DirectoryTreeInput
|
|
762
|
+
truncation_suggestions: ClassVar[List[str]] = [
|
|
763
|
+
"Use max_depth=2 to limit directory traversal depth",
|
|
764
|
+
"Use max_items=50 to limit total items returned",
|
|
765
|
+
"Target a specific subdirectory instead of the root",
|
|
766
|
+
]
|
|
767
|
+
|
|
768
|
+
# Track item count during tree building
|
|
769
|
+
_item_count: int = 0
|
|
770
|
+
_max_items: Optional[int] = None
|
|
771
|
+
_truncated: bool = False
|
|
772
|
+
|
|
773
|
+
def _build_tree(self, directory: Path, prefix: str = "", depth: int = 0, max_depth: Optional[int] = None) -> List[str]:
|
|
774
|
+
"""Recursively build directory tree with item limit."""
|
|
775
|
+
# Check depth limit
|
|
776
|
+
if max_depth is not None and depth >= max_depth:
|
|
777
|
+
return []
|
|
778
|
+
|
|
779
|
+
# Check item limit
|
|
780
|
+
if self._max_items is not None and self._item_count >= self._max_items:
|
|
781
|
+
if not self._truncated:
|
|
782
|
+
self._truncated = True
|
|
783
|
+
return []
|
|
784
|
+
|
|
785
|
+
lines = []
|
|
786
|
+
try:
|
|
787
|
+
entries = sorted(directory.iterdir(), key=lambda x: (not x.is_dir(), x.name.lower()))
|
|
788
|
+
|
|
789
|
+
for i, entry in enumerate(entries):
|
|
790
|
+
# Check item limit before adding each entry
|
|
791
|
+
if self._max_items is not None and self._item_count >= self._max_items:
|
|
792
|
+
if not self._truncated:
|
|
793
|
+
self._truncated = True
|
|
794
|
+
break
|
|
795
|
+
|
|
796
|
+
is_last = i == len(entries) - 1
|
|
797
|
+
current_prefix = "└── " if is_last else "├── "
|
|
798
|
+
next_prefix = " " if is_last else "│ "
|
|
799
|
+
|
|
800
|
+
self._item_count += 1
|
|
801
|
+
|
|
802
|
+
if entry.is_dir():
|
|
803
|
+
lines.append(f"{prefix}{current_prefix}📁 {entry.name}/")
|
|
804
|
+
lines.extend(self._build_tree(entry, prefix + next_prefix, depth + 1, max_depth))
|
|
805
|
+
else:
|
|
806
|
+
size = self._format_size(entry.stat().st_size)
|
|
807
|
+
lines.append(f"{prefix}{current_prefix}📄 {entry.name} ({size})")
|
|
808
|
+
except PermissionError:
|
|
809
|
+
lines.append(f"{prefix}[Permission Denied]")
|
|
810
|
+
|
|
811
|
+
return lines
|
|
812
|
+
|
|
813
|
+
def _run(self, path: str = ".", max_depth: Optional[int] = 3, max_items: Optional[int] = 200) -> str:
|
|
814
|
+
"""Get directory tree with size limits to prevent context overflow."""
|
|
815
|
+
try:
|
|
816
|
+
target = self._resolve_path(path)
|
|
817
|
+
|
|
818
|
+
if not target.exists():
|
|
819
|
+
return f"Error: Directory '{path}' does not exist"
|
|
820
|
+
|
|
821
|
+
if not target.is_dir():
|
|
822
|
+
return f"Error: '{path}' is not a directory"
|
|
823
|
+
|
|
824
|
+
# Reset counters for this run
|
|
825
|
+
self._item_count = 0
|
|
826
|
+
self._max_items = max_items
|
|
827
|
+
self._truncated = False
|
|
828
|
+
|
|
829
|
+
# Show relative path from base directory, use '.' for root
|
|
830
|
+
# This prevents confusion - files should be accessed relative to working directory
|
|
831
|
+
if path in (".", "", "./"):
|
|
832
|
+
display_root = "." # Root of working directory
|
|
833
|
+
else:
|
|
834
|
+
display_root = path.rstrip('/')
|
|
835
|
+
|
|
836
|
+
lines = [f"📁 {display_root}/"]
|
|
837
|
+
lines.extend(self._build_tree(target, "", 0, max_depth))
|
|
838
|
+
|
|
839
|
+
# Add truncation warning if limit was reached
|
|
840
|
+
if self._truncated:
|
|
841
|
+
lines.append("")
|
|
842
|
+
lines.append(f"⚠️ OUTPUT TRUNCATED: Showing {self._item_count} of more items (max_items={max_items}, max_depth={max_depth})")
|
|
843
|
+
lines.append(f" To see more: increase max_items or max_depth, or use filesystem_list_directory on specific subdirectories")
|
|
844
|
+
|
|
845
|
+
# Add note about file paths
|
|
846
|
+
lines.append("")
|
|
847
|
+
lines.append("Note: Use paths relative to working directory (e.g., 'agents/file.md', not including the root directory name)")
|
|
848
|
+
|
|
849
|
+
return "\n".join(lines)
|
|
850
|
+
except Exception as e:
|
|
851
|
+
return f"Error building directory tree for '{path}': {str(e)}"
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
class SearchFilesTool(FileSystemTool):
|
|
855
|
+
"""Search for files matching a pattern."""
|
|
856
|
+
name: str = "filesystem_search_files"
|
|
857
|
+
description: str = (
|
|
858
|
+
"Recursively search for files and directories matching a glob pattern. "
|
|
859
|
+
"Use patterns like '*.py' for Python files in current dir, or '**/*.py' for all Python files recursively. "
|
|
860
|
+
"Returns paths to matching items (default limit: 100 results to prevent context overflow). "
|
|
861
|
+
"Only searches within allowed directories."
|
|
862
|
+
)
|
|
863
|
+
args_schema: type[BaseModel] = SearchFilesInput
|
|
864
|
+
truncation_suggestions: ClassVar[List[str]] = [
|
|
865
|
+
"Use max_results=50 to limit number of results",
|
|
866
|
+
"Use a more specific glob pattern (e.g., 'src/**/*.py' instead of '**/*.py')",
|
|
867
|
+
"Search in a specific subdirectory instead of the root",
|
|
868
|
+
]
|
|
869
|
+
|
|
870
|
+
def _run(self, path: str = ".", pattern: str = "*", max_results: Optional[int] = 100) -> str:
|
|
871
|
+
"""Search for files with result limit."""
|
|
872
|
+
try:
|
|
873
|
+
target = self._resolve_path(path)
|
|
874
|
+
|
|
875
|
+
if not target.exists():
|
|
876
|
+
return f"Error: Directory '{path}' does not exist"
|
|
877
|
+
|
|
878
|
+
if not target.is_dir():
|
|
879
|
+
return f"Error: '{path}' is not a directory"
|
|
880
|
+
|
|
881
|
+
# Use glob to find matching files
|
|
882
|
+
all_matches = list(target.glob(pattern))
|
|
883
|
+
total_count = len(all_matches)
|
|
884
|
+
|
|
885
|
+
if not all_matches:
|
|
886
|
+
return f"No files matching '{pattern}' found in '{path}'"
|
|
887
|
+
|
|
888
|
+
# Apply limit
|
|
889
|
+
truncated = False
|
|
890
|
+
if max_results is not None and total_count > max_results:
|
|
891
|
+
matches = sorted(all_matches)[:max_results]
|
|
892
|
+
truncated = True
|
|
893
|
+
else:
|
|
894
|
+
matches = sorted(all_matches)
|
|
895
|
+
|
|
896
|
+
# Format results with directory prefixes for multi-directory configs
|
|
897
|
+
allowed_dirs = self._get_all_allowed_directories()
|
|
898
|
+
has_multiple_dirs = len(allowed_dirs) > 1
|
|
899
|
+
results = []
|
|
900
|
+
search_dir_name = None
|
|
901
|
+
|
|
902
|
+
for match in matches:
|
|
903
|
+
if has_multiple_dirs:
|
|
904
|
+
rel_path_str, dir_name = self._get_relative_path_from_allowed_dirs(match)
|
|
905
|
+
display_path = f"{dir_name}/{rel_path_str}"
|
|
906
|
+
if search_dir_name is None:
|
|
907
|
+
search_dir_name = dir_name
|
|
908
|
+
else:
|
|
909
|
+
rel_path_str = str(match.relative_to(Path(self.base_directory).resolve()))
|
|
910
|
+
display_path = rel_path_str
|
|
911
|
+
|
|
912
|
+
if match.is_dir():
|
|
913
|
+
results.append(f"📁 {display_path}/")
|
|
914
|
+
else:
|
|
915
|
+
size = self._format_size(match.stat().st_size)
|
|
916
|
+
results.append(f"📄 {display_path} ({size})")
|
|
917
|
+
|
|
918
|
+
header = f"Found {total_count} matches for '{pattern}':\n\n"
|
|
919
|
+
output = header + "\n".join(results)
|
|
920
|
+
|
|
921
|
+
if truncated:
|
|
922
|
+
location_str = f"from '{search_dir_name}' " if search_dir_name else ""
|
|
923
|
+
output += f"\n\n⚠️ OUTPUT TRUNCATED: Showing {max_results} of {total_count} results {location_str}(max_results={max_results})"
|
|
924
|
+
output += "\n To see more: increase max_results or use a more specific pattern"
|
|
925
|
+
|
|
926
|
+
return output
|
|
927
|
+
except Exception as e:
|
|
928
|
+
return f"Error searching files in '{path}': {str(e)}"
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
class DeleteFileTool(FileSystemTool):
|
|
932
|
+
"""Delete a file."""
|
|
933
|
+
name: str = "filesystem_delete_file"
|
|
934
|
+
description: str = (
|
|
935
|
+
"Delete a file. Use with caution as this operation cannot be undone. "
|
|
936
|
+
"Only deletes files, not directories. "
|
|
937
|
+
"Only works within allowed directories."
|
|
938
|
+
)
|
|
939
|
+
args_schema: type[BaseModel] = DeleteFileInput
|
|
940
|
+
|
|
941
|
+
def _run(self, path: str) -> str:
|
|
942
|
+
"""Delete a file."""
|
|
943
|
+
try:
|
|
944
|
+
target = self._resolve_path(path)
|
|
945
|
+
|
|
946
|
+
if not target.exists():
|
|
947
|
+
return f"Error: File '{path}' does not exist"
|
|
948
|
+
|
|
949
|
+
if not target.is_file():
|
|
950
|
+
return f"Error: '{path}' is not a file (directories cannot be deleted with this tool)"
|
|
951
|
+
|
|
952
|
+
size = target.stat().st_size
|
|
953
|
+
target.unlink()
|
|
954
|
+
|
|
955
|
+
return f"Successfully deleted '{path}' ({self._format_size(size)})"
|
|
956
|
+
except Exception as e:
|
|
957
|
+
return f"Error deleting file '{path}': {str(e)}"
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
class MoveFileTool(FileSystemTool):
|
|
961
|
+
"""Move or rename files and directories."""
|
|
962
|
+
name: str = "filesystem_move_file"
|
|
963
|
+
description: str = (
|
|
964
|
+
"Move or rename files and directories. Can move files between directories and rename them in a single operation. "
|
|
965
|
+
"If the destination exists, the operation will fail. "
|
|
966
|
+
"Works across different directories and can be used for simple renaming within the same directory. "
|
|
967
|
+
"Both source and destination must be within allowed directories."
|
|
968
|
+
)
|
|
969
|
+
args_schema: type[BaseModel] = MoveFileInput
|
|
970
|
+
|
|
971
|
+
def _run(self, source: str, destination: str) -> str:
|
|
972
|
+
"""Move or rename a file."""
|
|
973
|
+
try:
|
|
974
|
+
source_path = self._resolve_path(source)
|
|
975
|
+
dest_path = self._resolve_path(destination)
|
|
976
|
+
|
|
977
|
+
if not source_path.exists():
|
|
978
|
+
return f"Error: Source '{source}' does not exist"
|
|
979
|
+
|
|
980
|
+
if dest_path.exists():
|
|
981
|
+
return f"Error: Destination '{destination}' already exists"
|
|
982
|
+
|
|
983
|
+
# Create parent directories if needed
|
|
984
|
+
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
|
985
|
+
|
|
986
|
+
source_path.rename(dest_path)
|
|
987
|
+
|
|
988
|
+
return f"Successfully moved '{source}' to '{destination}'"
|
|
989
|
+
except Exception as e:
|
|
990
|
+
return f"Error moving '{source}' to '{destination}': {str(e)}"
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
class CreateDirectoryTool(FileSystemTool):
|
|
994
|
+
"""Create a directory."""
|
|
995
|
+
name: str = "filesystem_create_directory"
|
|
996
|
+
description: str = (
|
|
997
|
+
"Create a new directory or ensure a directory exists. "
|
|
998
|
+
"Can create multiple nested directories in one operation. "
|
|
999
|
+
"If the directory already exists, this operation will succeed silently. "
|
|
1000
|
+
"Only works within allowed directories."
|
|
1001
|
+
)
|
|
1002
|
+
args_schema: type[BaseModel] = CreateDirectoryInput
|
|
1003
|
+
|
|
1004
|
+
def _run(self, path: str) -> str:
|
|
1005
|
+
"""Create a directory."""
|
|
1006
|
+
try:
|
|
1007
|
+
target = self._resolve_path(path)
|
|
1008
|
+
|
|
1009
|
+
if target.exists():
|
|
1010
|
+
if target.is_dir():
|
|
1011
|
+
return f"Directory '{path}' already exists"
|
|
1012
|
+
else:
|
|
1013
|
+
return f"Error: '{path}' exists but is not a directory"
|
|
1014
|
+
|
|
1015
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
1016
|
+
|
|
1017
|
+
return f"Successfully created directory '{path}'"
|
|
1018
|
+
except Exception as e:
|
|
1019
|
+
return f"Error creating directory '{path}': {str(e)}"
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
class GetFileInfoTool(FileSystemTool):
|
|
1023
|
+
"""Get detailed file/directory information."""
|
|
1024
|
+
name: str = "filesystem_get_file_info"
|
|
1025
|
+
description: str = (
|
|
1026
|
+
"Retrieve detailed metadata about a file or directory. "
|
|
1027
|
+
"Returns comprehensive information including size, creation time, last modified time, permissions, and type. "
|
|
1028
|
+
"This tool is perfect for understanding file characteristics without reading the actual content. "
|
|
1029
|
+
"Only works within allowed directories."
|
|
1030
|
+
)
|
|
1031
|
+
args_schema: type[BaseModel] = GetFileInfoInput
|
|
1032
|
+
|
|
1033
|
+
def _run(self, path: str) -> str:
|
|
1034
|
+
"""Get file information."""
|
|
1035
|
+
try:
|
|
1036
|
+
target = self._resolve_path(path)
|
|
1037
|
+
|
|
1038
|
+
if not target.exists():
|
|
1039
|
+
return f"Error: Path '{path}' does not exist"
|
|
1040
|
+
|
|
1041
|
+
stat = target.stat()
|
|
1042
|
+
|
|
1043
|
+
info = {
|
|
1044
|
+
"Path": str(path),
|
|
1045
|
+
"Type": "Directory" if target.is_dir() else "File",
|
|
1046
|
+
"Size": self._format_size(stat.st_size) if target.is_file() else "N/A",
|
|
1047
|
+
"Created": datetime.fromtimestamp(stat.st_ctime).strftime("%Y-%m-%d %H:%M:%S"),
|
|
1048
|
+
"Modified": datetime.fromtimestamp(stat.st_mtime).strftime("%Y-%m-%d %H:%M:%S"),
|
|
1049
|
+
"Accessed": datetime.fromtimestamp(stat.st_atime).strftime("%Y-%m-%d %H:%M:%S"),
|
|
1050
|
+
"Permissions": oct(stat.st_mode)[-3:],
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
if target.is_file():
|
|
1054
|
+
info["Readable"] = os.access(target, os.R_OK)
|
|
1055
|
+
info["Writable"] = os.access(target, os.W_OK)
|
|
1056
|
+
info["Executable"] = os.access(target, os.X_OK)
|
|
1057
|
+
|
|
1058
|
+
return "\n".join(f"{key}: {value}" for key, value in info.items())
|
|
1059
|
+
except Exception as e:
|
|
1060
|
+
return f"Error getting info for '{path}': {str(e)}"
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
class ListAllowedDirectoriesTool(FileSystemTool):
|
|
1064
|
+
"""List allowed directories."""
|
|
1065
|
+
name: str = "filesystem_list_allowed_directories"
|
|
1066
|
+
description: str = (
|
|
1067
|
+
"Returns the list of directories that are accessible. "
|
|
1068
|
+
"Subdirectories within allowed directories are also accessible. "
|
|
1069
|
+
"Use this to understand which directories and their nested paths are available before trying to access files."
|
|
1070
|
+
)
|
|
1071
|
+
args_schema: type[BaseModel] = EmptyInput
|
|
1072
|
+
|
|
1073
|
+
def _run(self) -> str:
|
|
1074
|
+
"""List allowed directories."""
|
|
1075
|
+
dirs = self._get_all_allowed_directories()
|
|
1076
|
+
if len(dirs) == 1:
|
|
1077
|
+
return f"Allowed directory:\n{dirs[0]}\n\nAll subdirectories within this path are accessible."
|
|
1078
|
+
else:
|
|
1079
|
+
dir_list = "\n".join(f" - {d}" for d in dirs)
|
|
1080
|
+
return f"Allowed directories:\n{dir_list}\n\nAll subdirectories within these paths are accessible."
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
# ========== Filesystem API Wrapper for Inventory Ingestion ==========
|
|
1084
|
+
|
|
1085
|
+
class FilesystemApiWrapper:
|
|
1086
|
+
"""
|
|
1087
|
+
API Wrapper for filesystem operations compatible with inventory ingestion pipeline.
|
|
1088
|
+
|
|
1089
|
+
Supports both text and non-text files:
|
|
1090
|
+
- Text files: .py, .md, .txt, .json, .yaml, etc.
|
|
1091
|
+
- Documents: .pdf, .docx, .pptx, .xlsx, .xls (converted to markdown)
|
|
1092
|
+
- Images: .png, .jpg, .gif, .webp (base64 encoded or described via LLM)
|
|
1093
|
+
|
|
1094
|
+
Usage:
|
|
1095
|
+
# Create wrapper for a directory
|
|
1096
|
+
wrapper = FilesystemApiWrapper(base_directory="/path/to/docs")
|
|
1097
|
+
|
|
1098
|
+
# Load documents (uses inherited loader())
|
|
1099
|
+
for doc in wrapper.loader(whitelist=["*.md", "*.pdf"]):
|
|
1100
|
+
print(doc.page_content[:100])
|
|
1101
|
+
|
|
1102
|
+
# For image description, provide an LLM
|
|
1103
|
+
wrapper = FilesystemApiWrapper(base_directory="/path/to/docs", llm=my_llm)
|
|
1104
|
+
for doc in wrapper.loader(whitelist=["*.png"]):
|
|
1105
|
+
print(doc.page_content) # LLM-generated description
|
|
1106
|
+
|
|
1107
|
+
# Use with inventory ingestion
|
|
1108
|
+
pipeline = IngestionPipeline(llm=llm, graph_path="./graph.json")
|
|
1109
|
+
pipeline.register_toolkit("local_docs", wrapper)
|
|
1110
|
+
result = pipeline.run(source="local_docs", whitelist=["*.md", "*.pdf"])
|
|
1111
|
+
"""
|
|
1112
|
+
|
|
1113
|
+
# Filesystem-specific settings
|
|
1114
|
+
base_directory: str = ""
|
|
1115
|
+
recursive: bool = True
|
|
1116
|
+
follow_symlinks: bool = False
|
|
1117
|
+
llm: Any = None # Optional LLM for image processing
|
|
1118
|
+
|
|
1119
|
+
# File type categories
|
|
1120
|
+
BINARY_EXTENSIONS = {'.pdf', '.docx', '.doc', '.pptx', '.ppt', '.xlsx', '.xls'}
|
|
1121
|
+
IMAGE_EXTENSIONS = {'.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.svg'}
|
|
1122
|
+
|
|
1123
|
+
def __init__(
|
|
1124
|
+
self,
|
|
1125
|
+
base_directory: str,
|
|
1126
|
+
recursive: bool = True,
|
|
1127
|
+
follow_symlinks: bool = False,
|
|
1128
|
+
llm: Any = None,
|
|
1129
|
+
**kwargs
|
|
1130
|
+
):
|
|
1131
|
+
"""
|
|
1132
|
+
Initialize filesystem wrapper.
|
|
1133
|
+
|
|
1134
|
+
Args:
|
|
1135
|
+
base_directory: Root directory for file operations
|
|
1136
|
+
recursive: If True, search subdirectories recursively
|
|
1137
|
+
follow_symlinks: If True, follow symbolic links
|
|
1138
|
+
llm: Optional LLM for image description (if not provided, images are base64 encoded)
|
|
1139
|
+
**kwargs: Additional arguments (ignored, for compatibility)
|
|
1140
|
+
"""
|
|
1141
|
+
self.base_directory = str(Path(base_directory).resolve())
|
|
1142
|
+
self.recursive = recursive
|
|
1143
|
+
self.follow_symlinks = follow_symlinks
|
|
1144
|
+
self.llm = llm
|
|
1145
|
+
|
|
1146
|
+
# For compatibility with BaseCodeToolApiWrapper.loader()
|
|
1147
|
+
self.active_branch = None
|
|
1148
|
+
|
|
1149
|
+
# Validate directory
|
|
1150
|
+
if not Path(self.base_directory).exists():
|
|
1151
|
+
raise ValueError(f"Directory does not exist: {self.base_directory}")
|
|
1152
|
+
if not Path(self.base_directory).is_dir():
|
|
1153
|
+
raise ValueError(f"Path is not a directory: {self.base_directory}")
|
|
1154
|
+
|
|
1155
|
+
# Optional RunnableConfig for CLI/standalone usage
|
|
1156
|
+
self._runnable_config = None
|
|
1157
|
+
|
|
1158
|
+
def set_runnable_config(self, config: Optional[Dict[str, Any]]) -> None:
|
|
1159
|
+
"""
|
|
1160
|
+
Set the RunnableConfig for dispatching custom events.
|
|
1161
|
+
|
|
1162
|
+
This is required when running outside of a LangChain agent context
|
|
1163
|
+
(e.g., from CLI). Without a config containing a run_id,
|
|
1164
|
+
dispatch_custom_event will fail with "Unable to dispatch an adhoc event
|
|
1165
|
+
without a parent run id".
|
|
1166
|
+
|
|
1167
|
+
Args:
|
|
1168
|
+
config: A RunnableConfig dict with at least {'run_id': uuid}
|
|
1169
|
+
"""
|
|
1170
|
+
self._runnable_config = config
|
|
1171
|
+
|
|
1172
|
+
def _log_tool_event(self, message: str, tool_name: str = None, config: Optional[Dict[str, Any]] = None):
|
|
1173
|
+
"""Log progress events (mirrors BaseToolApiWrapper).
|
|
1174
|
+
|
|
1175
|
+
Args:
|
|
1176
|
+
message: The message to log
|
|
1177
|
+
tool_name: Name of the tool (defaults to 'filesystem')
|
|
1178
|
+
config: Optional RunnableConfig. If not provided, uses self._runnable_config.
|
|
1179
|
+
Required when running outside a LangChain agent context.
|
|
1180
|
+
"""
|
|
1181
|
+
logger.info(f"[{tool_name or 'filesystem'}] {message}")
|
|
1182
|
+
try:
|
|
1183
|
+
from langchain_core.callbacks import dispatch_custom_event
|
|
1184
|
+
|
|
1185
|
+
# Use provided config, fall back to instance config
|
|
1186
|
+
effective_config = config or getattr(self, '_runnable_config', None)
|
|
1187
|
+
|
|
1188
|
+
dispatch_custom_event(
|
|
1189
|
+
name="thinking_step",
|
|
1190
|
+
data={
|
|
1191
|
+
"message": message,
|
|
1192
|
+
"tool_name": tool_name or "filesystem",
|
|
1193
|
+
"toolkit": "FilesystemApiWrapper",
|
|
1194
|
+
},
|
|
1195
|
+
config=effective_config,
|
|
1196
|
+
)
|
|
1197
|
+
except Exception:
|
|
1198
|
+
pass
|
|
1199
|
+
|
|
1200
|
+
def _get_files(self, path: str = "", branch: str = None) -> List[str]:
|
|
1201
|
+
"""
|
|
1202
|
+
Get list of files in the directory.
|
|
1203
|
+
|
|
1204
|
+
Implements BaseCodeToolApiWrapper._get_files() for filesystem.
|
|
1205
|
+
|
|
1206
|
+
Args:
|
|
1207
|
+
path: Subdirectory path (relative to base_directory)
|
|
1208
|
+
branch: Ignored for filesystem (compatibility with git-based toolkits)
|
|
1209
|
+
|
|
1210
|
+
Returns:
|
|
1211
|
+
List of file paths relative to base_directory
|
|
1212
|
+
"""
|
|
1213
|
+
base = Path(self.base_directory)
|
|
1214
|
+
search_path = base / path if path else base
|
|
1215
|
+
|
|
1216
|
+
if not search_path.exists():
|
|
1217
|
+
return []
|
|
1218
|
+
|
|
1219
|
+
files = []
|
|
1220
|
+
|
|
1221
|
+
if self.recursive:
|
|
1222
|
+
for root, dirs, filenames in os.walk(search_path, followlinks=self.follow_symlinks):
|
|
1223
|
+
# Skip hidden directories
|
|
1224
|
+
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
|
1225
|
+
|
|
1226
|
+
for filename in filenames:
|
|
1227
|
+
if filename.startswith('.'):
|
|
1228
|
+
continue
|
|
1229
|
+
|
|
1230
|
+
full_path = Path(root) / filename
|
|
1231
|
+
try:
|
|
1232
|
+
rel_path = str(full_path.relative_to(base))
|
|
1233
|
+
files.append(rel_path)
|
|
1234
|
+
except ValueError:
|
|
1235
|
+
continue
|
|
1236
|
+
else:
|
|
1237
|
+
for entry in search_path.iterdir():
|
|
1238
|
+
if entry.is_file() and not entry.name.startswith('.'):
|
|
1239
|
+
try:
|
|
1240
|
+
rel_path = str(entry.relative_to(base))
|
|
1241
|
+
files.append(rel_path)
|
|
1242
|
+
except ValueError:
|
|
1243
|
+
continue
|
|
1244
|
+
|
|
1245
|
+
return sorted(files)
|
|
1246
|
+
|
|
1247
|
+
def _is_binary_file(self, file_path: str) -> bool:
|
|
1248
|
+
"""Check if file is a binary document (PDF, DOCX, etc.)."""
|
|
1249
|
+
ext = Path(file_path).suffix.lower()
|
|
1250
|
+
return ext in self.BINARY_EXTENSIONS
|
|
1251
|
+
|
|
1252
|
+
def _is_image_file(self, file_path: str) -> bool:
|
|
1253
|
+
"""Check if file is an image."""
|
|
1254
|
+
ext = Path(file_path).suffix.lower()
|
|
1255
|
+
return ext in self.IMAGE_EXTENSIONS
|
|
1256
|
+
|
|
1257
|
+
def _read_binary_file(self, file_path: str) -> Optional[str]:
|
|
1258
|
+
"""
|
|
1259
|
+
Read binary file (PDF, DOCX, PPTX, Excel) and convert to text/markdown.
|
|
1260
|
+
|
|
1261
|
+
Uses the SDK's content_parser for document conversion.
|
|
1262
|
+
|
|
1263
|
+
Args:
|
|
1264
|
+
file_path: Path relative to base_directory
|
|
1265
|
+
|
|
1266
|
+
Returns:
|
|
1267
|
+
Converted text content, or None if conversion fails
|
|
1268
|
+
"""
|
|
1269
|
+
full_path = Path(self.base_directory) / file_path
|
|
1270
|
+
|
|
1271
|
+
try:
|
|
1272
|
+
from alita_sdk.tools.utils.content_parser import parse_file_content
|
|
1273
|
+
|
|
1274
|
+
result = parse_file_content(
|
|
1275
|
+
file_path=str(full_path),
|
|
1276
|
+
is_capture_image=bool(self.llm), # Capture images if LLM available
|
|
1277
|
+
llm=self.llm
|
|
1278
|
+
)
|
|
1279
|
+
|
|
1280
|
+
if isinstance(result, Exception):
|
|
1281
|
+
logger.warning(f"Failed to parse {file_path}: {result}")
|
|
1282
|
+
return None
|
|
1283
|
+
|
|
1284
|
+
return result
|
|
1285
|
+
|
|
1286
|
+
except ImportError:
|
|
1287
|
+
logger.warning("content_parser not available, skipping binary file")
|
|
1288
|
+
return None
|
|
1289
|
+
except Exception as e:
|
|
1290
|
+
logger.warning(f"Error parsing {file_path}: {e}")
|
|
1291
|
+
return None
|
|
1292
|
+
|
|
1293
|
+
def _read_image_file(self, file_path: str) -> Optional[str]:
|
|
1294
|
+
"""
|
|
1295
|
+
Read image file and convert to text representation.
|
|
1296
|
+
|
|
1297
|
+
If LLM is available, uses it to describe the image.
|
|
1298
|
+
Otherwise, returns base64-encoded data URI.
|
|
1299
|
+
|
|
1300
|
+
Args:
|
|
1301
|
+
file_path: Path relative to base_directory
|
|
1302
|
+
|
|
1303
|
+
Returns:
|
|
1304
|
+
Image description or base64 data URI
|
|
1305
|
+
"""
|
|
1306
|
+
full_path = Path(self.base_directory) / file_path
|
|
1307
|
+
|
|
1308
|
+
if not full_path.exists():
|
|
1309
|
+
return None
|
|
1310
|
+
|
|
1311
|
+
ext = full_path.suffix.lower()
|
|
1312
|
+
|
|
1313
|
+
try:
|
|
1314
|
+
# Read image bytes
|
|
1315
|
+
image_bytes = full_path.read_bytes()
|
|
1316
|
+
|
|
1317
|
+
if self.llm:
|
|
1318
|
+
# Use content_parser with LLM for image description
|
|
1319
|
+
try:
|
|
1320
|
+
from alita_sdk.tools.utils.content_parser import parse_file_content
|
|
1321
|
+
|
|
1322
|
+
result = parse_file_content(
|
|
1323
|
+
file_path=str(full_path),
|
|
1324
|
+
is_capture_image=True,
|
|
1325
|
+
llm=self.llm
|
|
1326
|
+
)
|
|
1327
|
+
|
|
1328
|
+
if isinstance(result, Exception):
|
|
1329
|
+
logger.warning(f"Failed to describe image {file_path}: {result}")
|
|
1330
|
+
else:
|
|
1331
|
+
return f"[Image: {Path(file_path).name}]\n\n{result}"
|
|
1332
|
+
|
|
1333
|
+
except ImportError:
|
|
1334
|
+
pass
|
|
1335
|
+
|
|
1336
|
+
# Fallback: return base64 data URI
|
|
1337
|
+
mime_types = {
|
|
1338
|
+
'.png': 'image/png',
|
|
1339
|
+
'.jpg': 'image/jpeg',
|
|
1340
|
+
'.jpeg': 'image/jpeg',
|
|
1341
|
+
'.gif': 'image/gif',
|
|
1342
|
+
'.webp': 'image/webp',
|
|
1343
|
+
'.bmp': 'image/bmp',
|
|
1344
|
+
'.svg': 'image/svg+xml',
|
|
1345
|
+
}
|
|
1346
|
+
mime_type = mime_types.get(ext, 'application/octet-stream')
|
|
1347
|
+
b64_data = base64.b64encode(image_bytes).decode('utf-8')
|
|
1348
|
+
|
|
1349
|
+
return f"[Image: {Path(file_path).name}]\ndata:{mime_type};base64,{b64_data}"
|
|
1350
|
+
|
|
1351
|
+
except Exception as e:
|
|
1352
|
+
logger.warning(f"Error reading image {file_path}: {e}")
|
|
1353
|
+
return None
|
|
1354
|
+
|
|
1355
|
+
def _read_file(
|
|
1356
|
+
self,
|
|
1357
|
+
file_path: str,
|
|
1358
|
+
branch: str = None,
|
|
1359
|
+
offset: Optional[int] = None,
|
|
1360
|
+
limit: Optional[int] = None,
|
|
1361
|
+
head: Optional[int] = None,
|
|
1362
|
+
tail: Optional[int] = None,
|
|
1363
|
+
) -> Optional[str]:
|
|
1364
|
+
"""
|
|
1365
|
+
Read file content, handling text, binary documents, and images.
|
|
1366
|
+
|
|
1367
|
+
Supports:
|
|
1368
|
+
- Text files: Read directly with encoding detection
|
|
1369
|
+
- Binary documents (PDF, DOCX, PPTX, Excel): Convert to markdown
|
|
1370
|
+
- Images: Return LLM description or base64 data URI
|
|
1371
|
+
|
|
1372
|
+
Args:
|
|
1373
|
+
file_path: Path relative to base_directory
|
|
1374
|
+
branch: Ignored for filesystem (compatibility with git-based toolkits)
|
|
1375
|
+
offset: Start line number (1-indexed). If None, start from beginning.
|
|
1376
|
+
limit: Maximum number of lines to read. If None, read to end.
|
|
1377
|
+
head: Read only first N lines (alternative to offset/limit)
|
|
1378
|
+
tail: Read only last N lines (alternative to offset/limit)
|
|
1379
|
+
|
|
1380
|
+
Returns:
|
|
1381
|
+
File content as string, or None if unreadable
|
|
1382
|
+
"""
|
|
1383
|
+
full_path = Path(self.base_directory) / file_path
|
|
1384
|
+
|
|
1385
|
+
# Security check - prevent path traversal
|
|
1386
|
+
try:
|
|
1387
|
+
full_path.resolve().relative_to(Path(self.base_directory).resolve())
|
|
1388
|
+
except ValueError:
|
|
1389
|
+
logger.warning(f"Access denied: {file_path} is outside base directory")
|
|
1390
|
+
return None
|
|
1391
|
+
|
|
1392
|
+
if not full_path.exists() or not full_path.is_file():
|
|
1393
|
+
return None
|
|
1394
|
+
|
|
1395
|
+
# Route to appropriate reader based on file type
|
|
1396
|
+
# Note: offset/limit only apply to text files
|
|
1397
|
+
if self._is_binary_file(file_path):
|
|
1398
|
+
return self._read_binary_file(file_path)
|
|
1399
|
+
|
|
1400
|
+
if self._is_image_file(file_path):
|
|
1401
|
+
return self._read_image_file(file_path)
|
|
1402
|
+
|
|
1403
|
+
# Default: read as text with encoding detection
|
|
1404
|
+
encodings = ['utf-8', 'utf-8-sig', 'latin-1', 'cp1252']
|
|
1405
|
+
|
|
1406
|
+
for encoding in encodings:
|
|
1407
|
+
try:
|
|
1408
|
+
content = full_path.read_text(encoding=encoding)
|
|
1409
|
+
|
|
1410
|
+
# Apply line filtering if specified
|
|
1411
|
+
if offset is not None or limit is not None or head is not None or tail is not None:
|
|
1412
|
+
lines = content.splitlines(keepends=True)
|
|
1413
|
+
|
|
1414
|
+
if head is not None:
|
|
1415
|
+
# Read first N lines
|
|
1416
|
+
lines = lines[:head]
|
|
1417
|
+
elif tail is not None:
|
|
1418
|
+
# Read last N lines
|
|
1419
|
+
lines = lines[-tail:] if tail > 0 else []
|
|
1420
|
+
else:
|
|
1421
|
+
# Use offset/limit
|
|
1422
|
+
start_idx = (offset - 1) if offset and offset > 0 else 0
|
|
1423
|
+
if limit is not None:
|
|
1424
|
+
end_idx = start_idx + limit
|
|
1425
|
+
lines = lines[start_idx:end_idx]
|
|
1426
|
+
else:
|
|
1427
|
+
lines = lines[start_idx:]
|
|
1428
|
+
|
|
1429
|
+
content = ''.join(lines)
|
|
1430
|
+
|
|
1431
|
+
return content
|
|
1432
|
+
|
|
1433
|
+
except UnicodeDecodeError:
|
|
1434
|
+
continue
|
|
1435
|
+
except Exception as e:
|
|
1436
|
+
logger.warning(f"Failed to read {file_path}: {e}")
|
|
1437
|
+
return None
|
|
1438
|
+
|
|
1439
|
+
logger.warning(f"Could not decode {file_path} with any known encoding")
|
|
1440
|
+
return None
|
|
1441
|
+
|
|
1442
|
+
def read_file(
|
|
1443
|
+
self,
|
|
1444
|
+
file_path: str,
|
|
1445
|
+
offset: Optional[int] = None,
|
|
1446
|
+
limit: Optional[int] = None,
|
|
1447
|
+
head: Optional[int] = None,
|
|
1448
|
+
tail: Optional[int] = None,
|
|
1449
|
+
) -> Optional[str]:
|
|
1450
|
+
"""
|
|
1451
|
+
Public method to read file content with optional line range.
|
|
1452
|
+
|
|
1453
|
+
Args:
|
|
1454
|
+
file_path: Path relative to base_directory
|
|
1455
|
+
offset: Start line number (1-indexed)
|
|
1456
|
+
limit: Maximum number of lines to read
|
|
1457
|
+
head: Read only first N lines
|
|
1458
|
+
tail: Read only last N lines
|
|
1459
|
+
|
|
1460
|
+
Returns:
|
|
1461
|
+
File content as string
|
|
1462
|
+
"""
|
|
1463
|
+
return self._read_file(file_path, offset=offset, limit=limit, head=head, tail=tail)
|
|
1464
|
+
|
|
1465
|
+
def loader(
|
|
1466
|
+
self,
|
|
1467
|
+
branch: Optional[str] = None,
|
|
1468
|
+
whitelist: Optional[List[str]] = None,
|
|
1469
|
+
blacklist: Optional[List[str]] = None,
|
|
1470
|
+
chunked: bool = True,
|
|
1471
|
+
) -> Generator[Document, None, None]:
|
|
1472
|
+
"""
|
|
1473
|
+
Load documents from the filesystem.
|
|
1474
|
+
|
|
1475
|
+
Mirrors BaseCodeToolApiWrapper.loader() interface for compatibility.
|
|
1476
|
+
|
|
1477
|
+
Args:
|
|
1478
|
+
branch: Ignored (kept for API compatibility with git-based loaders)
|
|
1479
|
+
whitelist: File patterns to include (e.g., ['*.py', 'src/**/*.js'])
|
|
1480
|
+
blacklist: File patterns to exclude (e.g., ['*test*', 'node_modules/**'])
|
|
1481
|
+
chunked: If True, applies universal chunker based on file type
|
|
1482
|
+
|
|
1483
|
+
Yields:
|
|
1484
|
+
Document objects with page_content and metadata
|
|
1485
|
+
"""
|
|
1486
|
+
import glob as glob_module
|
|
1487
|
+
|
|
1488
|
+
base = Path(self.base_directory)
|
|
1489
|
+
|
|
1490
|
+
def is_blacklisted(file_path: str) -> bool:
|
|
1491
|
+
if not blacklist:
|
|
1492
|
+
return False
|
|
1493
|
+
return (
|
|
1494
|
+
any(fnmatch.fnmatch(file_path, p) for p in blacklist) or
|
|
1495
|
+
any(fnmatch.fnmatch(Path(file_path).name, p) for p in blacklist)
|
|
1496
|
+
)
|
|
1497
|
+
|
|
1498
|
+
# Optimization: Use glob directly when whitelist has path patterns
|
|
1499
|
+
# This avoids scanning 100K+ files in node_modules etc.
|
|
1500
|
+
def get_files_via_glob() -> Generator[str, None, None]:
|
|
1501
|
+
"""Use glob patterns directly - much faster than scanning all files."""
|
|
1502
|
+
seen = set()
|
|
1503
|
+
for pattern in whitelist:
|
|
1504
|
+
# Handle glob patterns
|
|
1505
|
+
full_pattern = str(base / pattern)
|
|
1506
|
+
for match in glob_module.glob(full_pattern, recursive=True):
|
|
1507
|
+
match_path = Path(match)
|
|
1508
|
+
if match_path.is_file():
|
|
1509
|
+
try:
|
|
1510
|
+
rel_path = str(match_path.relative_to(base))
|
|
1511
|
+
if rel_path not in seen and not is_blacklisted(rel_path):
|
|
1512
|
+
seen.add(rel_path)
|
|
1513
|
+
yield rel_path
|
|
1514
|
+
except ValueError:
|
|
1515
|
+
continue
|
|
1516
|
+
|
|
1517
|
+
def get_files_via_scan() -> Generator[str, None, None]:
|
|
1518
|
+
"""Fall back to scanning all files when no whitelist or simple extension patterns."""
|
|
1519
|
+
_files = self._get_files()
|
|
1520
|
+
self._log_tool_event(f"Found {len(_files)} files in {self.base_directory}", "loader")
|
|
1521
|
+
|
|
1522
|
+
def is_whitelisted(file_path: str) -> bool:
|
|
1523
|
+
if not whitelist:
|
|
1524
|
+
return True
|
|
1525
|
+
return (
|
|
1526
|
+
any(fnmatch.fnmatch(file_path, p) for p in whitelist) or
|
|
1527
|
+
any(fnmatch.fnmatch(Path(file_path).name, p) for p in whitelist) or
|
|
1528
|
+
any(file_path.endswith(f'.{p.lstrip("*.")}') for p in whitelist if p.startswith('*.'))
|
|
1529
|
+
)
|
|
1530
|
+
|
|
1531
|
+
for file_path in _files:
|
|
1532
|
+
if is_whitelisted(file_path) and not is_blacklisted(file_path):
|
|
1533
|
+
yield file_path
|
|
1534
|
+
|
|
1535
|
+
# Decide strategy: use glob if whitelist has path patterns (contains / or **)
|
|
1536
|
+
use_glob = whitelist and any('/' in p or '**' in p for p in whitelist)
|
|
1537
|
+
|
|
1538
|
+
if use_glob:
|
|
1539
|
+
self._log_tool_event(f"Using glob patterns: {whitelist}", "loader")
|
|
1540
|
+
file_iterator = get_files_via_glob()
|
|
1541
|
+
else:
|
|
1542
|
+
file_iterator = get_files_via_scan()
|
|
1543
|
+
|
|
1544
|
+
def raw_document_generator() -> Generator[Document, None, None]:
|
|
1545
|
+
self._log_tool_event("Reading files...", "loader")
|
|
1546
|
+
processed = 0
|
|
1547
|
+
|
|
1548
|
+
for file_path in file_iterator:
|
|
1549
|
+
content = self._read_file(file_path)
|
|
1550
|
+
if not content:
|
|
1551
|
+
continue
|
|
1552
|
+
|
|
1553
|
+
content_hash = hashlib.sha256(content.encode('utf-8')).hexdigest()
|
|
1554
|
+
processed += 1
|
|
1555
|
+
|
|
1556
|
+
yield Document(
|
|
1557
|
+
page_content=content,
|
|
1558
|
+
metadata={
|
|
1559
|
+
'file_path': file_path,
|
|
1560
|
+
'file_name': Path(file_path).name,
|
|
1561
|
+
'source': file_path,
|
|
1562
|
+
'commit_hash': content_hash,
|
|
1563
|
+
}
|
|
1564
|
+
)
|
|
1565
|
+
|
|
1566
|
+
# Log progress every 100 files
|
|
1567
|
+
if processed % 100 == 0:
|
|
1568
|
+
logger.debug(f"[loader] Read {processed} files...")
|
|
1569
|
+
|
|
1570
|
+
self._log_tool_event(f"Loaded {processed} files", "loader")
|
|
1571
|
+
|
|
1572
|
+
if not chunked:
|
|
1573
|
+
return raw_document_generator()
|
|
1574
|
+
|
|
1575
|
+
try:
|
|
1576
|
+
from alita_sdk.tools.chunkers.universal_chunker import universal_chunker
|
|
1577
|
+
return universal_chunker(raw_document_generator())
|
|
1578
|
+
except ImportError:
|
|
1579
|
+
logger.warning("Universal chunker not available, returning raw documents")
|
|
1580
|
+
return raw_document_generator()
|
|
1581
|
+
|
|
1582
|
+
def chunker(self, documents: Generator[Document, None, None]) -> Generator[Document, None, None]:
|
|
1583
|
+
"""Apply universal chunker to documents."""
|
|
1584
|
+
try:
|
|
1585
|
+
from alita_sdk.tools.chunkers.universal_chunker import universal_chunker
|
|
1586
|
+
return universal_chunker(documents)
|
|
1587
|
+
except ImportError:
|
|
1588
|
+
return documents
|
|
1589
|
+
|
|
1590
|
+
def get_files_content(self, file_path: str) -> Optional[str]:
|
|
1591
|
+
"""Get file content (compatibility alias for retrieval toolkit)."""
|
|
1592
|
+
return self._read_file(file_path)
|
|
1593
|
+
|
|
1594
|
+
|
|
1595
|
+
# Predefined tool presets for common use cases
|
|
1596
|
+
FILESYSTEM_TOOL_PRESETS = {
|
|
1597
|
+
'read_only': {
|
|
1598
|
+
'exclude_tools': [
|
|
1599
|
+
'filesystem_write_file',
|
|
1600
|
+
'filesystem_append_file',
|
|
1601
|
+
'filesystem_edit_file',
|
|
1602
|
+
'filesystem_apply_patch',
|
|
1603
|
+
'filesystem_delete_file',
|
|
1604
|
+
'filesystem_move_file',
|
|
1605
|
+
'filesystem_create_directory',
|
|
1606
|
+
]
|
|
1607
|
+
},
|
|
1608
|
+
'no_delete': {
|
|
1609
|
+
'exclude_tools': ['filesystem_delete_file']
|
|
1610
|
+
},
|
|
1611
|
+
'basic': {
|
|
1612
|
+
'include_tools': [
|
|
1613
|
+
'filesystem_read_file',
|
|
1614
|
+
'filesystem_write_file',
|
|
1615
|
+
'filesystem_append_file',
|
|
1616
|
+
'filesystem_list_directory',
|
|
1617
|
+
'filesystem_create_directory',
|
|
1618
|
+
]
|
|
1619
|
+
},
|
|
1620
|
+
'minimal': {
|
|
1621
|
+
'include_tools': [
|
|
1622
|
+
'filesystem_read_file',
|
|
1623
|
+
'filesystem_list_directory',
|
|
1624
|
+
]
|
|
1625
|
+
},
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
|
|
1629
|
+
def get_filesystem_tools(
|
|
1630
|
+
base_directory: str,
|
|
1631
|
+
include_tools: Optional[List[str]] = None,
|
|
1632
|
+
exclude_tools: Optional[List[str]] = None,
|
|
1633
|
+
preset: Optional[str] = None,
|
|
1634
|
+
allowed_directories: Optional[List[str]] = None
|
|
1635
|
+
) -> List[BaseTool]:
|
|
1636
|
+
"""
|
|
1637
|
+
Get filesystem tools for the specified directories.
|
|
1638
|
+
|
|
1639
|
+
Args:
|
|
1640
|
+
base_directory: Absolute or relative path to the primary directory to restrict access to
|
|
1641
|
+
include_tools: Optional list of tool names to include. If provided, only these tools are returned.
|
|
1642
|
+
If None, all tools are included (unless excluded).
|
|
1643
|
+
exclude_tools: Optional list of tool names to exclude. Applied after include_tools.
|
|
1644
|
+
preset: Optional preset name to use predefined tool sets. Presets:
|
|
1645
|
+
- 'read_only': Excludes all write/modify operations
|
|
1646
|
+
- 'no_delete': All tools except delete
|
|
1647
|
+
- 'basic': Read, write, append, list, create directory
|
|
1648
|
+
- 'minimal': Only read and list
|
|
1649
|
+
Note: If preset is used with include_tools or exclude_tools,
|
|
1650
|
+
preset is applied first, then custom filters.
|
|
1651
|
+
|
|
1652
|
+
Returns:
|
|
1653
|
+
List of filesystem tools based on preset and/or include/exclude filters
|
|
1654
|
+
|
|
1655
|
+
Available tool names:
|
|
1656
|
+
- filesystem_read_file
|
|
1657
|
+
- filesystem_read_file_chunk
|
|
1658
|
+
- filesystem_read_multiple_files
|
|
1659
|
+
- filesystem_write_file
|
|
1660
|
+
- filesystem_append_file (for incremental file creation)
|
|
1661
|
+
- filesystem_edit_file
|
|
1662
|
+
- filesystem_apply_patch
|
|
1663
|
+
- filesystem_list_directory
|
|
1664
|
+
- filesystem_directory_tree
|
|
1665
|
+
- filesystem_search_files
|
|
1666
|
+
- filesystem_delete_file
|
|
1667
|
+
- filesystem_move_file
|
|
1668
|
+
- filesystem_create_directory
|
|
1669
|
+
- filesystem_get_file_info
|
|
1670
|
+
- filesystem_list_allowed_directories
|
|
1671
|
+
|
|
1672
|
+
Examples:
|
|
1673
|
+
# Get all tools
|
|
1674
|
+
get_filesystem_tools('/path/to/dir')
|
|
1675
|
+
|
|
1676
|
+
# Only read operations
|
|
1677
|
+
get_filesystem_tools('/path/to/dir',
|
|
1678
|
+
include_tools=['filesystem_read_file', 'filesystem_list_directory'])
|
|
1679
|
+
|
|
1680
|
+
# All tools except delete and write
|
|
1681
|
+
get_filesystem_tools('/path/to/dir',
|
|
1682
|
+
exclude_tools=['filesystem_delete_file', 'filesystem_write_file'])
|
|
1683
|
+
|
|
1684
|
+
# Use preset for read-only mode
|
|
1685
|
+
get_filesystem_tools('/path/to/dir', preset='read_only')
|
|
1686
|
+
|
|
1687
|
+
# Use preset and add custom exclusions
|
|
1688
|
+
get_filesystem_tools('/path/to/dir', preset='read_only',
|
|
1689
|
+
exclude_tools=['filesystem_search_files'])
|
|
1690
|
+
|
|
1691
|
+
# Multiple allowed directories
|
|
1692
|
+
get_filesystem_tools('/path/to/primary',
|
|
1693
|
+
allowed_directories=['/path/to/other1', '/path/to/other2'])
|
|
1694
|
+
"""
|
|
1695
|
+
# Apply preset if specified
|
|
1696
|
+
preset_include = None
|
|
1697
|
+
preset_exclude = None
|
|
1698
|
+
if preset:
|
|
1699
|
+
if preset not in FILESYSTEM_TOOL_PRESETS:
|
|
1700
|
+
raise ValueError(f"Unknown preset '{preset}'. Available: {list(FILESYSTEM_TOOL_PRESETS.keys())}")
|
|
1701
|
+
preset_config = FILESYSTEM_TOOL_PRESETS[preset]
|
|
1702
|
+
preset_include = preset_config.get('include_tools')
|
|
1703
|
+
preset_exclude = preset_config.get('exclude_tools')
|
|
1704
|
+
|
|
1705
|
+
# Merge preset with custom filters
|
|
1706
|
+
# Priority: custom include_tools > preset include > all tools
|
|
1707
|
+
final_include = include_tools if include_tools is not None else preset_include
|
|
1708
|
+
|
|
1709
|
+
# Priority: custom exclude_tools + preset exclude
|
|
1710
|
+
final_exclude = []
|
|
1711
|
+
if preset_exclude:
|
|
1712
|
+
final_exclude.extend(preset_exclude)
|
|
1713
|
+
if exclude_tools:
|
|
1714
|
+
final_exclude.extend(exclude_tools)
|
|
1715
|
+
final_exclude = list(set(final_exclude)) if final_exclude else None
|
|
1716
|
+
|
|
1717
|
+
# Resolve to absolute paths
|
|
1718
|
+
base_dir = str(Path(base_directory).resolve())
|
|
1719
|
+
extra_dirs = [str(Path(d).resolve()) for d in (allowed_directories or [])]
|
|
1720
|
+
|
|
1721
|
+
# Define all available tools with their names
|
|
1722
|
+
all_tools = {
|
|
1723
|
+
'filesystem_read_file': ReadFileTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1724
|
+
'filesystem_read_file_chunk': ReadFileChunkTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1725
|
+
'filesystem_read_multiple_files': ReadMultipleFilesTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1726
|
+
'filesystem_write_file': WriteFileTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1727
|
+
'filesystem_append_file': AppendFileTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1728
|
+
'filesystem_edit_file': EditFileTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1729
|
+
'filesystem_apply_patch': ApplyPatchTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1730
|
+
'filesystem_list_directory': ListDirectoryTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1731
|
+
'filesystem_directory_tree': DirectoryTreeTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1732
|
+
'filesystem_search_files': SearchFilesTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1733
|
+
'filesystem_delete_file': DeleteFileTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1734
|
+
'filesystem_move_file': MoveFileTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1735
|
+
'filesystem_create_directory': CreateDirectoryTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1736
|
+
'filesystem_get_file_info': GetFileInfoTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1737
|
+
'filesystem_list_allowed_directories': ListAllowedDirectoriesTool(base_directory=base_dir, allowed_directories=extra_dirs),
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
# Start with all tools or only included ones
|
|
1741
|
+
if final_include is not None:
|
|
1742
|
+
selected_tools = {name: tool for name, tool in all_tools.items() if name in final_include}
|
|
1743
|
+
else:
|
|
1744
|
+
selected_tools = all_tools.copy()
|
|
1745
|
+
|
|
1746
|
+
# Remove excluded tools
|
|
1747
|
+
if final_exclude is not None:
|
|
1748
|
+
for name in final_exclude:
|
|
1749
|
+
selected_tools.pop(name, None)
|
|
1750
|
+
|
|
1751
|
+
return list(selected_tools.values())
|