Repository Public
1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/recorders/knowledge.py4# Description: Records the fixtures of knowledge health, the brief, living blocks (POST /api/query, the five sources), semantic5# zoom (levels 2, 3 and the 501 of level 1), tags and parent cycles (API-NEXT §4.5–4.7, §4.9, §4.15).6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9import os10import sys11import time12import urllib.parse1314sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))15from spn_recorder import Recorder, new_id # noqa: E402161718def text(value):19 return {"type": "paragraph", "content": [{"type": "text", "text": value}]}202122def now():23 return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()) + ".%03dZ" % int((time.time() % 1) * 1000)242526def q(value):27 return urllib.parse.quote(value, safe="")282930r = Recorder("knowledge")31pages = []323334def page(body):35 made = r.call("POST", "/api/pages", dict({"id": new_id()}, **body)).json36 pages.append(made["id"])37 return made383940project = page({"title": "Fixture · savoir · projet", "section": "projects"})41ref = r.call("POST", "/api/research/references", {"id": new_id(), "title": "Fixture · Econometric analysis", "authors": ["Greene, William H."], "year": 2018}).json42task_id = new_id()43b = page({"title": "Fixture · savoir · données", "parentId": project["id"], "section": "projects",44 "content": {"type": "doc", "content": [text("Les séries couvrent 2010 à 2025 et proviennent de la banque centrale.")]}})45a = page({"title": "Fixture · savoir · revue", "parentId": project["id"], "section": "projects", "content": {"type": "doc", "content": [46 text("Introduction avant le premier titre. Elle tient en deux phrases."),47 {"type": "heading", "attrs": {"level": 1}, "content": [{"type": "text", "text": "Volatilité conditionnelle"}]},48 {"type": "paragraph", "content": [{"type": "text", "text": "Le manuel de référence est "},49 {"type": "citation", "attrs": {"refId": ref["id"], "label": "Greene 2018"}},50 {"type": "text", "text": ". Voir aussi "}, {"type": "mention", "attrs": {"id": b["id"], "label": "Données"}},51 {"type": "text", "text": " puis une page disparue "}, {"type": "mention", "attrs": {"id": new_id(), "label": "Fantôme"}},52 {"type": "text", "text": " et une source absente "}, {"type": "citation", "attrs": {"refId": new_id(), "label": "Nul 1900"}},53 {"type": "text", "text": "."}]},54 {"type": "heading", "attrs": {"level": 2}, "content": [{"type": "text", "text": "Tâches"}]},55 {"type": "taskList", "content": [{"type": "taskItem", "attrs": {"checked": False, "taskId": task_id, "due": "2026-11-25", "priority": "high"},56 "content": [text("Relire la section 2")]}]}]}})57twin1 = page({"title": "Fixture · savoir · doublon", "section": "notes", "content": {"type": "doc", "content": [text("Un deux trois quatre cinq six sept.")]}})58twin2 = page({"title": "Fixture · savoir · doublon", "section": "notes", "content": {"type": "doc", "content": [text("Un deux trois quatre cinq six sept.")]}})59untitled = page({"title": "", "section": "notes"})60r.call("PUT", "/api/pages/%s/tags" % a["id"], {"tags": ["fixture-savoir", "fixture-revue"]})61r.call("PUT", "/api/pages/%s/tags" % b["id"], {"tags": ["fixture-savoir"]})62late = r.call("POST", "/api/tasks", {"id": new_id(), "title": "Fixture · tâche en retard", "due": "2026-01-15", "priority": "low", "pageId": b["id"]}).json63loose = r.call("POST", "/api/tasks", {"id": new_id(), "title": "Fixture · tâche sans date"}).json64database = page({"title": "Fixture · savoir · lectures", "type": "database", "section": "databases"})65prop = r.call("POST", "/api/databases/%s/properties" % database["id"], {"id": new_id(), "name": "Année", "type": "number"}).json66row = r.call("POST", "/api/databases/%s/rows" % database["id"], {"id": new_id(), "pageId": new_id(), "title": "Fixture · Engle (1982)",67 "values": {prop["id"]: 1982}}).json68pages.append(row["pageId"])69capture = r.call("POST", "/api/capture", {"kind": "note", "text": "Fixture · capture à classer"}).json70pages.append(capture["id"])7172# --- health73r.call("GET", "/api/health/knowledge", save="health")74r.call("GET", "/api/health/knowledge?inactiveDays=1", save="health-inactive-1")75r.call("GET", "/api/health/knowledge?inactiveDays=0", save="health-bad-days")7677# --- brief (GET only: POST /api/brief/summary calls a model and is never requested here)78r.call("GET", "/api/brief?tz=America/Toronto", save="brief")79r.call("GET", "/api/brief?tz=Pacific/Auckland", save="brief-auckland")80r.call("GET", "/api/brief", save="brief-default")81r.call("GET", "/api/brief?tz=Mars/Olympus", save="brief-bad-tz")8283# --- living blocks84r.call("POST", "/api/query", {"source": "tasks", "filter": {"parentId": a["id"], "status": "all"}, "limit": 10}, save="query-tasks")85r.call("POST", "/api/query", {"source": "tasks", "filter": {"dueBefore": "2026-06-01", "text": "Fixture"}}, save="query-tasks-due")86r.call("POST", "/api/query", {"source": "pages", "filter": {"tag": "fixture-savoir"}, "limit": 5}, save="query-pages")87r.call("POST", "/api/query", {"source": "pages", "filter": {"parentId": project["id"]}, "limit": 1}, save="query-pages-limit")88r.call("POST", "/api/query", {"source": "database", "databaseId": database["id"], "limit": 10}, save="query-database")89r.call("POST", "/api/query", {"source": "references", "filter": {"text": "Econometric"}}, save="query-references")90r.call("POST", "/api/query", {"source": "backlinks", "pageId": b["id"]}, save="query-backlinks")91r.call("POST", "/api/query", {"source": "database"}, save="query-no-database")92r.call("POST", "/api/query", {"source": "database", "databaseId": new_id()}, save="query-database-not-found")93r.call("POST", "/api/query", {"source": "oracle"}, save="query-validation")94r.call("POST", "/api/query", {"source": "tasks", "filter": {"status": "maybe"}}, save="query-bad-status")95r.call("POST", "/api/query", {"source": "tasks", "filter": {"dueBefore": "soon"}}, save="query-bad-date")96r.call("POST", "/api/query", {"source": "backlinks"}, save="query-no-page")9798# --- zoom99r.call("GET", "/api/pages/%s/zoom?level=2" % a["id"], save="zoom-2")100r.call("GET", "/api/pages/%s/zoom" % a["id"], save="zoom-default")101r.call("GET", "/api/pages/%s/zoom?level=3" % a["id"], save="zoom-3")102r.call("GET", "/api/pages/%s/zoom?level=1" % a["id"], save="zoom-1")103r.call("GET", "/api/pages/%s/zoom?level=7" % a["id"], save="zoom-bad-level")104r.call("GET", "/api/pages/%s/zoom?level=2" % untitled["id"], save="zoom-2-empty")105r.call("GET", "/api/pages/%s/zoom?level=2" % new_id(), save="zoom-not-found")106107# --- tags, cycles108r.call("GET", "/api/tags", save="tags")109r.call("PATCH", "/api/pages/" + project["id"], {"parentId": a["id"]}, save="page-cycle")110r.call("PATCH", "/api/pages/" + project["id"], {"parentId": new_id()}, save="page-bad-parent")111r.call("PATCH", "/api/pages/" + project["id"], {"parentId": project["id"]}, save="page-parent-self")112r.call("GET", "/api/pages/not-a-uuid", save="page-bad-id")113114# --- clean up115for tid in (task_id, late["id"], loose["id"]):116 r.call("DELETE", "/api/tasks/" + tid)117r.call("DELETE", "/api/research/references/" + ref["id"])118r.purge_pages(pages[::-1])119r.finish()120