01629780f4
Code du systeme de memoire multi-LLM sur Trilium : - trilium_api.py : wrapper trilium-py (notes, labels, relations) - mcp_server.py : serveur MCP Starlette (19 tools, OAuth + Bearer) - api_context.py : API REST FastAPI - trilium_context.py : workflow CLI - watchdog.sh, start_*.sh : supervision et demarrage - skills, docs et ontologie associes Secrets (.env, oauth_state.json) exclus via .gitignore.
246 lines
14 KiB
Python
246 lines
14 KiB
Python
import json, os
|
|
from trilium_api import create_note, get_note_id, set_label, find_note_by_title, update_note_content
|
|
|
|
with open(os.path.expanduser('~/App/Context_continuity/trilium_ids.json')) as f:
|
|
ids = json.load(f)
|
|
|
|
root_id = ids['root']
|
|
|
|
# ── Dossier Documentation ──────────────────────────────────────────────────
|
|
doc_id = find_note_by_title('Documentation', root_id)
|
|
if not doc_id:
|
|
res = create_note(root_id, 'Documentation', note_type='book')
|
|
doc_id = get_note_id(res)
|
|
set_label(doc_id, 'type', 'container')
|
|
print('Dossier Documentation cree:', doc_id)
|
|
else:
|
|
print('Dossier Documentation existant:', doc_id)
|
|
|
|
# ── Note index Documentation ───────────────────────────────────────────────
|
|
res = create_note(doc_id, 'API Context Continuity - Vue densemble')
|
|
nid = get_note_id(res)
|
|
set_label(nid, 'type', 'documentation')
|
|
set_label(nid, 'projet', 'ContextContinuity')
|
|
update_note_content(nid,
|
|
'<h1>API Context Continuity</h1>'
|
|
'<p><b>Base URL externe</b> : <code>https://api-trilium.bertha-cloud.fr</code><br>'
|
|
'<b>Base URL locale</b> : <code>http://localhost:8765</code><br>'
|
|
'<b>Doc Swagger</b> : <a href="https://api-trilium.bertha-cloud.fr/api/docs">https://api-trilium.bertha-cloud.fr/api/docs</a><br>'
|
|
'<b>Auth</b> : Header <code>Authorization: <API_KEY></code></p>'
|
|
'<h2>Ressources disponibles</h2>'
|
|
'<table>'
|
|
'<tr><th>Ressource</th><th>Endpoints</th><th>Note Trilium</th></tr>'
|
|
'<tr><td>Projets</td><td>GET /api/projets, GET /api/projets/{nom}, POST, PATCH</td><td>Documentation > Projets</td></tr>'
|
|
'<tr><td>Backlog</td><td>GET /api/backlog/{projet}, POST /api/backlog, PATCH</td><td>Documentation > Backlog</td></tr>'
|
|
'<tr><td>Decisions</td><td>GET /api/decisions/{projet}, POST, PATCH</td><td>Documentation > Decisions</td></tr>'
|
|
'<tr><td>Historique</td><td>GET /api/historique/{projet}, POST, PATCH</td><td>Documentation > Historique</td></tr>'
|
|
'<tr><td>Conversations</td><td>GET /api/conversations/{projet}, POST, PATCH</td><td>Documentation > Conversations</td></tr>'
|
|
'<tr><td>Glossaire</td><td>GET /api/glossaire/{projet}, POST, PATCH</td><td>Documentation > Glossaire</td></tr>'
|
|
'<tr><td>Contexte Reprise</td><td>GET /api/contexte/{projet}, GET /api/contexte/{projet}/last</td><td>Documentation > Contexte Reprise</td></tr>'
|
|
'</table>'
|
|
'<h2>Codes de retour</h2>'
|
|
'<table>'
|
|
'<tr><th>Code</th><th>Signification</th></tr>'
|
|
'<tr><td>200</td><td>OK</td></tr>'
|
|
'<tr><td>201</td><td>Cree avec succes</td></tr>'
|
|
'<tr><td>401</td><td>Cle API invalide</td></tr>'
|
|
'<tr><td>404</td><td>Ressource introuvable</td></tr>'
|
|
'<tr><td>422</td><td>Validation Pydantic echouee (parametre invalide)</td></tr>'
|
|
'</table>'
|
|
)
|
|
print('Index documentation cree:', nid)
|
|
|
|
# ── Notes par ressource ────────────────────────────────────────────────────
|
|
ressources = [
|
|
(
|
|
'API - Projets',
|
|
'<h1>API - Projets</h1>'
|
|
'<h2>GET /api/projets</h2>'
|
|
'<p>Liste tous les projets.</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/projets</pre>'
|
|
'<h2>GET /api/projets/{nom}</h2>'
|
|
'<p>Detail d un projet par son nom (valeur du label #projet).</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/projets/SlidingAutomation</pre>'
|
|
'<h2>POST /api/projets</h2>'
|
|
'<p>Cree un nouveau projet.</p>'
|
|
'<pre>curl -s -X POST -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"nom":"MonProjet","objectif":"Description","stack":"Python","llm_repartition":"Claude → archi"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/projets</pre>'
|
|
'<h2>PATCH /api/projets/{note_id}</h2>'
|
|
'<p>Modifie le statut ou l objectif. Champs optionnels : statut (actif|en-pause|archive), objectif, stack.</p>'
|
|
'<pre>curl -s -X PATCH -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"statut":"en-pause"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/projets/NOTE_ID</pre>'
|
|
),
|
|
(
|
|
'API - Backlog',
|
|
'<h1>API - Backlog</h1>'
|
|
'<h2>GET /api/backlog/{projet}</h2>'
|
|
'<p>Liste les items actifs (hors fait et abandonne), tries par priorite.</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/backlog/SlidingAutomation</pre>'
|
|
'<h2>POST /api/backlog</h2>'
|
|
'<p>Cree un item. Champs : projet (requis), titre (requis), priorite (haute|moyenne|basse, defaut: moyenne).</p>'
|
|
'<pre>curl -s -X POST -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"projet":"SlidingAutomation","titre":"Ma tache","priorite":"haute"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/backlog</pre>'
|
|
'<h2>PATCH /api/backlog/{note_id}</h2>'
|
|
'<p>Modifie statut ou priorite. statut valide : a faire|en cours|bloque|fait|abandonne.</p>'
|
|
'<pre>curl -s -X PATCH -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"statut":"fait"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/backlog/NOTE_ID</pre>'
|
|
),
|
|
(
|
|
'API - Decisions',
|
|
'<h1>API - Decisions</h1>'
|
|
'<h2>GET /api/decisions/{projet}</h2>'
|
|
'<p>Liste les decisions actives (statut=active).</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/decisions/SlidingAutomation</pre>'
|
|
'<h2>POST /api/decisions</h2>'
|
|
'<p>Cree une decision. Champs : projet, enonce (requis), justification, llm.</p>'
|
|
'<pre>curl -s -X POST -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"projet":"SlidingAutomation","enonce":"On utilise X","justification":"Parce que Y"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/decisions</pre>'
|
|
'<h2>PATCH /api/decisions/{note_id}</h2>'
|
|
'<p>Modifie statut (active|revisee|annulee) ou justification.</p>'
|
|
'<pre>curl -s -X PATCH -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"statut":"annulee"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/decisions/NOTE_ID</pre>'
|
|
),
|
|
(
|
|
'API - Historique',
|
|
'<h1>API - Historique</h1>'
|
|
'<h2>GET /api/historique/{projet}</h2>'
|
|
'<p>Liste les entrees encore valides (encoreValide=true).</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/historique/SlidingAutomation</pre>'
|
|
'<h2>POST /api/historique</h2>'
|
|
'<p>Cree une entree. type valide : Fait etabli|Test effectue|Hypothese invalidee|Contrainte decouverte.</p>'
|
|
'<pre>curl -s -X POST -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"projet":"SlidingAutomation","enonce":"Test X","type":"Test effectue","detail":"Resultat Y"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/historique</pre>'
|
|
'<h2>PATCH /api/historique/{note_id}</h2>'
|
|
'<p>Invalider une entree obsolete : encore_valide: false.</p>'
|
|
'<pre>curl -s -X PATCH -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"encore_valide":false}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/historique/NOTE_ID</pre>'
|
|
),
|
|
(
|
|
'API - Conversations',
|
|
'<h1>API - Conversations</h1>'
|
|
'<h2>GET /api/conversations/{projet}</h2>'
|
|
'<p>Liste toutes les conversations du projet.</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/conversations/SlidingAutomation</pre>'
|
|
'<h2>GET /api/conversations/{projet}/{note_id}</h2>'
|
|
'<p>Detail avec contenu HTML d une conversation specifique.</p>'
|
|
'<h2>POST /api/conversations</h2>'
|
|
'<p>Cree une conversation. llm valide : Claude Sonnet|Claude Opus|Le Chat Large|Le Chat Medium.</p>'
|
|
'<pre>curl -s -X POST -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"projet":"SlidingAutomation","titre":"Ma session","llm":"Claude Sonnet"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/conversations</pre>'
|
|
'<h2>PATCH /api/conversations/{note_id}</h2>'
|
|
'<p>Cloture une session : synthese_cloture (max 500 chars) et/ou statut (en-cours|clos).</p>'
|
|
'<pre>curl -s -X PATCH -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"synthese_cloture":"Session terminee. Reste X."}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/conversations/NOTE_ID</pre>'
|
|
),
|
|
(
|
|
'API - Glossaire',
|
|
'<h1>API - Glossaire</h1>'
|
|
'<h2>GET /api/glossaire/{projet}</h2>'
|
|
'<p>Liste les termes avec contenu (definition).</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" https://api-trilium.bertha-cloud.fr/api/glossaire/SlidingAutomation</pre>'
|
|
'<h2>POST /api/glossaire</h2>'
|
|
'<p>Cree un terme. Champs : projet, terme, definition (requis), synonymes_eviter.</p>'
|
|
'<pre>curl -s -X POST -H "Authorization: CLE" -H "Content-Type: application/json" \\\n'
|
|
'-d \'{"projet":"SlidingAutomation","terme":"Pipeline","definition":"Chaine complete de traitement"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/glossaire</pre>'
|
|
'<h2>PATCH /api/glossaire/{note_id}</h2>'
|
|
'<p>Modifie definition et/ou synonymes_eviter.</p>'
|
|
),
|
|
(
|
|
'API - Contexte Reprise',
|
|
'<h1>API - Contexte Reprise</h1>'
|
|
'<h2>GET /api/contexte/{projet}?llm_cible=NomLLM</h2>'
|
|
'<p>Genere et retourne le briefing de reprise en temps reel depuis Trilium.</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" \\\n'
|
|
'"https://api-trilium.bertha-cloud.fr/api/contexte/SlidingAutomation?llm_cible=Le+Chat+Large"</pre>'
|
|
'<p>Retourne : {"projet":"...","tokens":N,"briefing":"# REPRISE DE CONTEXTE..."}</p>'
|
|
'<h2>GET /api/contexte/{projet}/last</h2>'
|
|
'<p>Retourne le dernier contexte de reprise sauvegarde dans Trilium.</p>'
|
|
'<pre>curl -s -H "Authorization: CLE" \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/contexte/SlidingAutomation/last</pre>'
|
|
),
|
|
]
|
|
|
|
for titre, contenu in ressources:
|
|
res = create_note(doc_id, titre)
|
|
nid = get_note_id(res)
|
|
set_label(nid, 'type', 'documentation')
|
|
set_label(nid, 'projet', 'ContextContinuity')
|
|
update_note_content(nid, contenu)
|
|
print('Doc cree:', titre)
|
|
|
|
# ── Skill API FastAPI pour LLMs ────────────────────────────────────────────
|
|
skills_id = find_note_by_title('Skills', root_id)
|
|
res = create_note(skills_id, 'Skill - API FastAPI Context Continuity')
|
|
nid = get_note_id(res)
|
|
set_label(nid, 'type', 'skill')
|
|
set_label(nid, 'domaine', 'api')
|
|
set_label(nid, 'version', '1.0')
|
|
|
|
update_note_content(nid,
|
|
'<h1>Skill - API FastAPI Context Continuity</h1>'
|
|
'<p>Ce skill permet a un LLM d appeler directement l API pour lire et ecrire dans Trilium.</p>'
|
|
'<h2>Configuration</h2>'
|
|
'<table>'
|
|
'<tr><th>Parametre</th><th>Valeur</th></tr>'
|
|
'<tr><td>Base URL</td><td>https://api-trilium.bertha-cloud.fr</td></tr>'
|
|
'<tr><td>Auth header</td><td>Authorization: <API_KEY></td></tr>'
|
|
'<tr><td>Cle Claude</td><td>Demander a Bastien ou lire dans .env</td></tr>'
|
|
'<tr><td>Cle Le Chat</td><td>Demander a Bastien ou lire dans .env</td></tr>'
|
|
'<tr><td>Doc Swagger</td><td>https://api-trilium.bertha-cloud.fr/api/docs</td></tr>'
|
|
'</table>'
|
|
'<h2>Endpoints essentiels pour un LLM</h2>'
|
|
'<table>'
|
|
'<tr><th>Action</th><th>Appel</th></tr>'
|
|
'<tr><td>Lire le contexte projet</td><td>GET /api/contexte/{projet}?llm_cible=NomLLM</td></tr>'
|
|
'<tr><td>Voir le backlog</td><td>GET /api/backlog/{projet}</td></tr>'
|
|
'<tr><td>Enregistrer une decision</td><td>POST /api/decisions</td></tr>'
|
|
'<tr><td>Enregistrer un historique</td><td>POST /api/historique</td></tr>'
|
|
'<tr><td>Creer une conversation</td><td>POST /api/conversations</td></tr>'
|
|
'<tr><td>Cloturer une session</td><td>PATCH /api/conversations/{id}</td></tr>'
|
|
'<tr><td>Marquer backlog item fait</td><td>PATCH /api/backlog/{id}</td></tr>'
|
|
'</table>'
|
|
'<h2>Valeurs valides par champ</h2>'
|
|
'<table>'
|
|
'<tr><th>Champ</th><th>Valeurs acceptees</th></tr>'
|
|
'<tr><td>priorite</td><td>haute | moyenne | basse</td></tr>'
|
|
'<tr><td>statut backlog</td><td>a faire | en cours | bloque | fait | abandonne</td></tr>'
|
|
'<tr><td>statut decision</td><td>active | revisee | annulee</td></tr>'
|
|
'<tr><td>type historique</td><td>Fait etabli | Test effectue | Hypothese invalidee | Contrainte decouverte</td></tr>'
|
|
'<tr><td>llm conversation</td><td>Claude Sonnet | Claude Opus | Le Chat Large | Le Chat Medium</td></tr>'
|
|
'<tr><td>statut projet</td><td>actif | en-pause | archive</td></tr>'
|
|
'</table>'
|
|
'<h2>Exemples curl complets</h2>'
|
|
'<h3>Lire le contexte de reprise</h3>'
|
|
'<pre>curl -s -H "Authorization: CLE" \\\n'
|
|
'"https://api-trilium.bertha-cloud.fr/api/contexte/SlidingAutomation?llm_cible=Le+Chat+Large" \\\n'
|
|
'| python3 -c "import sys,json; print(json.load(sys.stdin)[\'briefing\'])"</pre>'
|
|
'<h3>Enregistrer une decision</h3>'
|
|
'<pre>curl -s -X POST \\\n'
|
|
'-H "Authorization: CLE" \\\n'
|
|
'-H "Content-Type: application/json" \\\n'
|
|
'-d \'{"projet":"SlidingAutomation","enonce":"Decision prise","justification":"Raison"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/decisions</pre>'
|
|
'<h3>Marquer un item backlog comme fait</h3>'
|
|
'<pre>curl -s -X PATCH \\\n'
|
|
'-H "Authorization: CLE" \\\n'
|
|
'-H "Content-Type: application/json" \\\n'
|
|
'-d \'{"statut":"fait"}\' \\\n'
|
|
'https://api-trilium.bertha-cloud.fr/api/backlog/NOTE_ID</pre>'
|
|
'<h2>Recuperer ce skill</h2>'
|
|
'<pre>curl -s -H "Authorization: CLE" \\\n'
|
|
'"https://api-trilium.bertha-cloud.fr/api/contexte/ContextContinuity/last"</pre>'
|
|
)
|
|
print('Skill API FastAPI cree:', nid)
|
|
print('DONE')
|