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

84 lines
3.0 KiB
Python

"""Helpers to extract used graph element IDs from retriever results for session QA."""
from typing import Any, Dict, List, Optional
def is_edge_list(obj: Any) -> bool:
"""Duck-type: list of objects with node1, node2, attributes (CogneeGraph Edge)."""
if not isinstance(obj, list) or not obj:
return False
first = obj[0]
return (
getattr(first, "node1", None) is not None
and getattr(first, "node2", None) is not None
and getattr(first, "attributes", None) is not None
)
def extract_from_edges(edges: List[Any]) -> Optional[Dict[str, List[str]]]:
"""
From a list of Edge-like objects, collect node_ids and edge_ids.
edge_ids only from attributes["edge_object_id"] when present (no fallback).
Returns None if nothing extracted.
"""
node_ids: set[str] = set()
edge_ids: set[str] = set()
for edge in edges:
try:
n1 = getattr(edge, "node1", None)
n2 = getattr(edge, "node2", None)
attrs = getattr(edge, "attributes", None) or {}
if n1 is not None and getattr(n1, "id", None) is not None:
node_ids.add(str(n1.id))
if n2 is not None and getattr(n2, "id", None) is not None:
node_ids.add(str(n2.id))
eid = attrs.get("edge_object_id")
if eid is not None:
edge_ids.add(str(eid))
except (TypeError, AttributeError):
continue
result: Dict[str, List[str]] = {}
if node_ids:
result["node_ids"] = sorted(node_ids)
if edge_ids:
result["edge_ids"] = sorted(edge_ids)
return result if result else None
def extract_from_scored_results(results: List[Any]) -> Optional[Dict[str, List[str]]]:
"""
From ScoredResult-like list: node_ids from payload["id"] if present, else .id.
No edge_ids. Returns None if nothing extracted.
"""
node_ids: set[str] = set()
for r in results:
try:
pid = None
if getattr(r, "payload", None) and isinstance(r.payload, dict):
pid = r.payload.get("id")
if pid is not None:
node_ids.add(str(pid))
elif getattr(r, "id", None) is not None:
node_ids.add(str(r.id))
except (TypeError, AttributeError):
continue
if not node_ids:
return None
return {"node_ids": sorted(node_ids)}
def extract_from_temporal_dict(obj: Dict[str, Any]) -> Optional[Dict[str, List[str]]]:
"""
From temporal retriever dict: triplets -> extract_from_edges; events path -> extract_from_scored_results.
Returns None if nothing extracted.
"""
if not isinstance(obj, dict):
return None
triplets = obj.get("triplets")
if triplets is not None and is_edge_list(triplets):
return extract_from_edges(triplets)
scored_results = obj.get("vector_search_results")
if isinstance(scored_results, list) and scored_results:
return extract_from_scored_results(scored_results)
return None