Skip to content

Commit e8d11fd

Browse files
committed
feat: list files internal tool
1 parent 72393dc commit e8d11fd

3 files changed

Lines changed: 300 additions & 26 deletions

File tree

application/agents/agentic_agent.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
from application.agents.base import BaseAgent
55
from application.agents.tools.internal_search import (
6-
INTERNAL_TOOL_ENTRY,
76
INTERNAL_TOOL_ID,
87
build_internal_tool_config,
8+
build_internal_tool_entry,
99
)
1010
from application.logging import LogContext
1111

@@ -39,9 +39,13 @@ def _gen_inner(
3939
source = self.retriever_config.get("source", {})
4040
has_sources = bool(source.get("active_docs"))
4141
if self.retriever_config and has_sources:
42-
internal_entry = dict(INTERNAL_TOOL_ENTRY)
42+
has_dir = _sources_have_directory_structure(source)
43+
internal_entry = build_internal_tool_entry(
44+
has_directory_structure=has_dir
45+
)
4346
internal_entry["config"] = build_internal_tool_config(
44-
**self.retriever_config
47+
**self.retriever_config,
48+
has_directory_structure=has_dir,
4549
)
4650
tools_dict[INTERNAL_TOOL_ID] = internal_entry
4751

@@ -74,3 +78,40 @@ def _collect_internal_sources(self):
7478
tool = self.tool_executor._loaded_tools.get(cache_key)
7579
if tool and hasattr(tool, "retrieved_docs") and tool.retrieved_docs:
7680
self.retrieved_docs = tool.retrieved_docs
81+
82+
83+
def _sources_have_directory_structure(source: Dict) -> bool:
84+
"""Check if any of the active sources have directory_structure in MongoDB."""
85+
active_docs = source.get("active_docs", [])
86+
if not active_docs:
87+
return False
88+
89+
try:
90+
from bson.objectid import ObjectId
91+
from application.core.mongo_db import MongoDB
92+
93+
mongo = MongoDB.get_client()
94+
db = mongo[settings.MONGO_DB_NAME]
95+
sources_collection = db["sources"]
96+
97+
if isinstance(active_docs, str):
98+
active_docs = [active_docs]
99+
100+
for doc_id in active_docs:
101+
try:
102+
source_doc = sources_collection.find_one(
103+
{"_id": ObjectId(doc_id)},
104+
{"directory_structure": 1},
105+
)
106+
if source_doc and source_doc.get("directory_structure"):
107+
return True
108+
except Exception:
109+
continue
110+
except Exception as e:
111+
logger.debug(f"Could not check directory structure: {e}")
112+
113+
return False
114+
115+
116+
# Import settings at module level for _sources_have_directory_structure
117+
from application.core.settings import settings # noqa: E402

application/agents/research_agent.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
from application.agents.base import BaseAgent
88
from application.agents.tool_executor import ToolExecutor
9+
from application.agents.agentic_agent import _sources_have_directory_structure
910
from application.agents.tools.internal_search import (
10-
INTERNAL_TOOL_ENTRY,
1111
INTERNAL_TOOL_ID,
1212
build_internal_tool_config,
13+
build_internal_tool_entry,
1314
)
1415
from application.agents.tools.think import THINK_TOOL_ENTRY, THINK_TOOL_ID
1516
from application.logging import LogContext
@@ -275,9 +276,13 @@ def _setup_tools(self) -> Dict:
275276
source = self.retriever_config.get("source", {})
276277
has_sources = bool(source.get("active_docs"))
277278
if self.retriever_config and has_sources:
278-
internal_entry = dict(INTERNAL_TOOL_ENTRY)
279+
has_dir = _sources_have_directory_structure(source)
280+
internal_entry = build_internal_tool_entry(
281+
has_directory_structure=has_dir
282+
)
279283
internal_entry["config"] = build_internal_tool_config(
280-
**self.retriever_config
284+
**self.retriever_config,
285+
has_directory_structure=has_dir,
281286
)
282287
tools_dict[INTERNAL_TOOL_ID] = internal_entry
283288
elif self.retriever_config and not has_sources:

0 commit comments

Comments
 (0)