|
3 | 3 |
|
4 | 4 | from application.agents.base import BaseAgent |
5 | 5 | from application.agents.tools.internal_search import ( |
6 | | - INTERNAL_TOOL_ENTRY, |
7 | 6 | INTERNAL_TOOL_ID, |
8 | 7 | build_internal_tool_config, |
| 8 | + build_internal_tool_entry, |
9 | 9 | ) |
10 | 10 | from application.logging import LogContext |
11 | 11 |
|
@@ -39,9 +39,13 @@ def _gen_inner( |
39 | 39 | source = self.retriever_config.get("source", {}) |
40 | 40 | has_sources = bool(source.get("active_docs")) |
41 | 41 | 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 | + ) |
43 | 46 | internal_entry["config"] = build_internal_tool_config( |
44 | | - **self.retriever_config |
| 47 | + **self.retriever_config, |
| 48 | + has_directory_structure=has_dir, |
45 | 49 | ) |
46 | 50 | tools_dict[INTERNAL_TOOL_ID] = internal_entry |
47 | 51 |
|
@@ -74,3 +78,40 @@ def _collect_internal_sources(self): |
74 | 78 | tool = self.tool_executor._loaded_tools.get(cache_key) |
75 | 79 | if tool and hasattr(tool, "retrieved_docs") and tool.retrieved_docs: |
76 | 80 | 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 |
0 commit comments