Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

81 lines
2.6 KiB
Python

from typing import Any, Optional
from cognee.infrastructure.databases.vector.exceptions import CollectionNotFoundError
from cognee.modules.retrieval.utils.brute_force_triplet_search import get_memory_fragment
# Vector collection for the persisted GlobalContextSummary.text index.
GLOBAL_CONTEXT_SUMMARY_COLLECTION = "GlobalContextSummary_text"
async def load_root_text() -> Optional[str]:
"""
Return the dataset's root GlobalContextSummary text, or None when no root
exists in scope. Relies on backend access control to scope the query to
one dataset; within scope a dataset has at most one root.
"""
fragment = await get_memory_fragment(
properties_to_project=["id", "text", "type", "is_root"],
memory_fragment_filter=[{"type": ["GlobalContextSummary"]}],
)
for node in fragment.nodes.values():
if _is_root(node.attributes):
text = node.attributes.get("text")
return text or None
return None
async def search_top_global_context_summaries(
query: str,
top_k: int,
vector_engine: Any,
query_vector: Optional[list[float]] = None,
) -> list[str]:
"""Return up to top_k non-root GlobalContextSummary texts ranked by similarity."""
if top_k <= 0:
return []
try:
results = await vector_engine.search(
GLOBAL_CONTEXT_SUMMARY_COLLECTION,
None if query_vector is not None else query,
query_vector=query_vector,
limit=top_k + 1, # +1 in case the root is in the top results
include_payload=True,
)
except CollectionNotFoundError:
return []
summaries: list[str] = []
for result in results or []:
payload = getattr(result, "payload", None) or {}
if _is_root(payload):
continue
text = payload.get("text")
if text:
summaries.append(text)
if len(summaries) >= top_k:
break
return summaries
def format_global_context_prelude(
root_text: Optional[str],
top_summaries: list[str],
) -> str:
"""Build the prepend string. Empty when neither input has content."""
blocks: list[str] = []
if root_text:
blocks.append(f"World summary:\n{root_text}")
if top_summaries:
joined = "\n\n".join(top_summaries)
blocks.append(f"Relevant areas:\n{joined}")
return "\n\n".join(blocks)
def _is_root(attributes: dict) -> bool:
value = attributes.get("is_root")
if isinstance(value, bool):
return value
if isinstance(value, str):
return value.lower() == "true"
return bool(value)