Repository Public
1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/recorders/graph.py4# Description: Records the fixtures of the knowledge graph 2.0 (API-NEXT §4.3): typed relations, concepts, the seven graph5# views and the 2.x form, insights — success and error answers. Creates its own pages and removes them.6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9import os10import sys1112sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))13from spn_recorder import Recorder, new_id # noqa: E402141516def text(value):17 return {"type": "paragraph", "content": [{"type": "text", "text": value}]}181920r = Recorder("graph")21pages = []222324def page(body):25 made = r.call("POST", "/api/pages", dict({"id": new_id()}, **body)).json26 pages.append(made["id"])27 return made282930project = page({"title": "Fixture · graphe · projet", "section": "projects"})31ref = r.call("POST", "/api/research/references", {"id": new_id(), "title": "Fixture · Autoregressive conditional heteroscedasticity",32 "authors": ["Engle, Robert F."], "year": 1982, "journal": "Econometrica"}).json33b = page({"title": "Fixture · graphe · données", "parentId": project["id"], "section": "projects",34 "content": {"type": "doc", "content": [text("Les séries couvrent 2010 à 2025 et sont décrites en détail ici.")]}})35a = page({"title": "Fixture · graphe · revue", "parentId": project["id"], "section": "projects", "content": {"type": "doc", "content": [36 {"type": "heading", "attrs": {"level": 1}, "content": [{"type": "text", "text": "Volatilité"}]},37 {"type": "paragraph", "content": [{"type": "text", "text": "Le modèle fondateur est "},38 {"type": "citation", "attrs": {"refId": ref["id"], "locator": "p. 987", "label": "Engle 1982"}},39 {"type": "text", "text": ". Voir aussi "}, {"type": "mention", "attrs": {"id": b["id"], "label": "Données"}},40 {"type": "text", "text": "."}]}]}})41c = page({"title": "Fixture · graphe · méthode", "parentId": project["id"], "section": "projects",42 "content": {"type": "doc", "content": [text("Estimation par maximum de vraisemblance.")]}})43doc = page({"title": "Fixture · graphe · document", "section": "documents"})44for _ in range(2): # two pages with the same title and the same text: a duplicate for the insights45 page({"title": "Fixture · graphe · doublon", "section": "notes", "content": {"type": "doc", "content": [text("Un deux trois quatre cinq six sept huit.")]}})46for target, tags in ((a, ["fixture-graphe", "fixture-garch"]), (b, ["fixture-graphe"]), (c, ["fixture-graphe"])):47 r.call("PUT", "/api/pages/%s/tags" % target["id"], {"tags": tags})48att = r.upload("/api/attachments", {"id": new_id(), "pageId": a["id"]}, "fixture-graphe.txt", "text/plain", b"fixture graph attachment\n").json4950# --- relations51rel_id = new_id()52relation = {"id": rel_id, "fromId": b["id"], "toId": a["id"], "relType": "supports", "note": "Les données appuient la revue",53 "validFrom": "2026-01-01T00:00:00Z", "validTo": "2026-12-31T00:00:00-05:00", "sourceBlockId": None}54r.call("POST", "/api/graph/relations", relation, save="relation-create")55r.call("POST", "/api/graph/relations", dict(relation, note="retry"), save="relation-create-existing")56r.call("POST", "/api/graph/relations", {"fromId": b["id"], "toId": a["id"], "relType": "supports"}, save="relation-exists")57r.call("POST", "/api/graph/relations", {"fromId": b["id"], "toId": new_id(), "relType": "related"}, save="relation-bad-node")58r.call("POST", "/api/graph/relations", {"fromId": b["id"], "toId": b["id"], "relType": "related"}, save="relation-bad-node-same")59r.call("POST", "/api/graph/relations", {"fromId": b["id"], "toId": c["id"], "relType": "related", "validFrom": "2026-06-01T00:00:00Z",60 "validTo": "2026-01-01T00:00:00Z"}, save="relation-bad-validity")61r.call("POST", "/api/graph/relations", {"fromId": b["id"], "toId": c["id"], "relType": "loves"}, save="relation-validation")62second = r.call("POST", "/api/graph/relations", {"fromId": c["id"], "toId": a["id"], "relType": "precedes"}, save="relation-create-minimal").json63r.call("PATCH", "/api/graph/relations/" + rel_id, {"relType": "extends", "note": None, "validTo": None}, save="relation-patch")64r.call("PATCH", "/api/graph/relations/" + rel_id, {"status": "rejected"}, save="relation-patch-rejected")65r.call("PATCH", "/api/graph/relations/" + rel_id, {"status": "accepted"}, save="relation-patch-accepted")66r.call("PATCH", "/api/graph/relations/" + second["id"], {"relType": "wrong"}, save="relation-patch-validation")67r.call("GET", "/api/graph/relations/" + rel_id, save="relation-get")68r.call("GET", "/api/graph/relations/" + new_id(), save="relation-not-found")69r.call("GET", "/api/graph/relations/not-a-uuid", save="relation-bad-id")70listed = r.call("GET", "/api/graph/relations?nodeId=%s&include=cites,link,ai_suggested" % a["id"], save="relations-list-include").json71r.call("GET", "/api/graph/relations?nodeId=%s" % a["id"], save="relations-list")72r.call("GET", "/api/graph/relations?relType=precedes&origin=user&status=accepted", save="relations-list-filtered")73derived = [edge for edge in listed["relations"] if edge["kind"] in ("link", "cites")]74if derived:75 r.call("PATCH", "/api/graph/relations/" + derived[0]["id"], {"note": "x"}, save="relation-derived-edge")76 r.call("DELETE", "/api/graph/relations/" + derived[0]["id"], save="relation-delete-derived")7778# --- concepts79concept_id = new_id()80concept = {"id": concept_id, "name": "Fixture · hétéroscédasticité", "description": "Variance non constante", "pageIds": [a["id"], b["id"]]}81r.call("POST", "/api/concepts", concept, save="concept-create")82r.call("POST", "/api/concepts", dict(concept, description="retry"), save="concept-create-existing")83r.call("POST", "/api/concepts", {"name": "fixture · HÉTÉROSCÉDASTICITÉ"}, save="concept-exists")84r.call("POST", "/api/concepts", {"name": "Fixture · concept sans page", "pageIds": [new_id()]}, save="concept-bad-page")85r.call("POST", "/api/concepts", {"name": ""}, save="concept-validation")86other = r.call("POST", "/api/concepts", {"name": "Fixture · stationnarité"}, save="concept-create-minimal").json87r.call("GET", "/api/concepts/" + concept_id, save="concept-get")88r.call("PATCH", "/api/concepts/" + concept_id, {"removePages": [b["id"]], "addPages": [c["id"]], "description": "Variance conditionnelle"},89 save="concept-patch")90r.call("PATCH", "/api/concepts/" + concept_id, {"pageLinks": [{"pageId": c["id"], "status": "rejected"}]}, save="concept-patch-page-links")91r.call("PATCH", "/api/concepts", {"id": other["id"], "status": "rejected"}, save="concept-patch-collection")92r.call("PATCH", "/api/concepts/" + concept_id, {"name": "Fixture · stationnarité"}, save="concept-patch-exists")93r.call("GET", "/api/concepts", save="concepts-list")94r.call("GET", "/api/concepts?status=all&pageId=%s" % a["id"], save="concepts-list-page")95r.call("GET", "/api/concepts?status=rejected", save="concepts-list-rejected")96r.call("GET", "/api/concepts/" + new_id(), save="concept-not-found")97# a relation between a page and a concept98r.call("POST", "/api/graph/relations", {"fromId": c["id"], "toId": concept_id, "targetKind": "concept", "relType": "example_of"},99 save="relation-create-concept")100101# --- views102r.call("GET", "/api/graph?view=local&pageId=%s&depth=2" % a["id"], save="graph-local")103r.call("GET", "/api/graph?view=project&pageId=%s" % project["id"], save="graph-project")104r.call("GET", "/api/graph?view=global&hideOrphans=1", save="graph-global")105r.call("GET", "/api/graph?view=global&suggestions=0", save="graph-global-no-suggestions")106r.call("GET", "/api/graph?view=timeline&from=2026-11-01T00:00:00Z&to=2027-01-01T00:00:00Z", save="graph-timeline")107r.call("GET", "/api/graph?view=concepts", save="graph-concepts")108r.call("GET", "/api/graph?view=documents", save="graph-documents")109r.call("GET", "/api/graph?view=references", save="graph-references")110r.call("GET", "/api/graph?view=galaxy", save="graph-bad-view")111r.call("GET", "/api/graph?view=local", save="graph-local-no-page")112r.call("GET", "/api/graph?view=local&pageId=%s&depth=9" % a["id"], save="graph-local-bad-depth")113r.call("GET", "/api/graph?view=project&pageId=%s" % new_id(), save="graph-project-not-found")114r.call("GET", "/api/graph?pageId=%s" % a["id"], save="graph-legacy-page")115r.call("GET", "/api/graph", save="graph-legacy")116117# --- insights (deterministic only: the AI part is a paid job and is never requested here)118r.call("POST", "/api/graph/insights/run", {"mode": "deterministic"}, save="insights-run")119r.call("POST", "/api/graph/insights/run", {"mode": "deterministic", "pageIds": [a["id"], b["id"], c["id"]], "limit": 5}, save="insights-run-again")120r.call("POST", "/api/graph/insights/run", {"mode": "psychic"}, save="insights-run-validation")121insights = r.call("GET", "/api/graph/insights?limit=10", save="insights").json122r.call("GET", "/api/graph/insights?status=all&intelligence=0&limit=5", save="insights-no-intelligence")123r.call("GET", "/api/graph/insights?kind=recurring_topic&status=suggested", save="insights-kind")124r.call("GET", "/api/graph/insights?status=never", save="insights-bad-status")125mine = [i for i in insights["insights"] if set(i["data"].get("pageIds", [])) & set(pages)]126if mine:127 r.call("PATCH", "/api/graph/insights/" + mine[0]["id"], {"status": "accepted"}, save="insight-accept")128 r.call("PATCH", "/api/graph/insights/" + mine[0]["id"], {"status": "dismissed"}, save="insight-dismiss")129 r.call("PATCH", "/api/graph/insights/" + mine[0]["id"], {"status": "later"}, save="insight-validation")130 for extra in mine[1:]:131 r.call("PATCH", "/api/graph/insights/" + extra["id"], {"status": "dismissed"})132r.call("PATCH", "/api/graph/insights/" + new_id(), {"status": "dismissed"}, save="insight-not-found")133134# --- clean up135r.call("DELETE", "/api/graph/relations/" + second["id"], save="relation-delete")136r.call("DELETE", "/api/graph/relations/" + second["id"], save="relation-delete-again")137r.call("DELETE", "/api/concepts/" + other["id"], save="concept-delete")138r.call("DELETE", "/api/concepts/" + other["id"], save="concept-delete-again")139r.call("DELETE", "/api/concepts/" + concept_id)140r.call("DELETE", "/api/graph/relations/" + rel_id)141r.call("DELETE", "/api/attachments?id=" + att["id"])142r.call("DELETE", "/api/research/references/" + ref["id"])143r.purge_pages(pages[::-1])144r.finish()145