Repository navigation
Expand file tree
/
Copy pathrecommit_test.py
More file actions
89 lines (75 loc) · 2.8 KB
/
Copy pathrecommit_test.py
File metadata and controls
89 lines (75 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
import asyncio
import sys
sys.path.insert(0, "c:/Users/caoha/OneDrive/Desktop/hackathon/backend")
from app.api.content_container import get_container
def main():
container = get_container()
with open("backend/test_agentic_result.json", "r", encoding="utf-8") as f:
run_result = json.load(f)
package = run_result["commit_package"]
concepts = package["concepts"]
edges = package["edges"]
# Apply connectivity logic
root_id = "agentic-design-patterns"
root_name = "Agentic Design Patterns"
# Add root if missing
if not any(c["concept_id"] == root_id for c in concepts):
concepts.append({
"concept_id": root_id,
"name": root_name,
"definition": "Core subject and overarching theme of the document.",
"source_refs": [{"page": 1, "snippet": "Document Theme"}],
"aliases": []
})
# Build adj and in_degree
adj = {c["concept_id"]: set() for c in concepts}
in_degree = {c["concept_id"]: 0 for c in concepts}
for e in edges:
s = e["source_concept_id"]
t = e["target_concept_id"]
adj[s].add(t)
adj[t].add(s)
in_degree[t] += 1
# Find components
visited = set()
components = []
for c in concepts:
cid = c["concept_id"]
if cid not in visited:
comp = set()
q = [cid]
while q:
curr = q.pop(0)
if curr not in visited:
visited.add(curr)
comp.add(curr)
for nxt in adj.get(curr, []):
if nxt not in visited:
q.append(nxt)
components.append(comp)
root_component = next((comp for comp in components if root_id in comp), set())
for comp in components:
if comp == root_component:
continue
local_roots = [node for node in comp if in_degree[node] == 0]
target_node = local_roots[0] if local_roots else list(comp)[0]
edges.append({
"source_concept_id": root_id,
"target_concept_id": target_node,
"relation_type": "prerequisite",
"reason": "Connects the main document theme to its sub-topics to ensure graph connectivity."
})
print("Re-committing from test_agentic_result.json with Master Root injected...")
try:
container.pdf_workflow_service.commit(
run_result=run_result,
graph_repo=container.graph_repo,
content_repo=container.content_repo,
)
print("Commit successful! Edges should now be populated.")
except Exception as e:
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()