Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9e3240ae2 | ||
|
|
0841778b99 | ||
|
|
788d84a2bf | ||
|
|
168261964e | ||
|
|
2f129c3f23 | ||
|
|
d98c0c2f33 | ||
|
|
3261917d62 | ||
|
|
e3df4bbf00 | ||
|
|
f630771150 | ||
|
|
431fb5626d | ||
|
|
19e12fa22e | ||
|
|
daf9ff3b2d | ||
|
|
1ed0d52619 | ||
|
|
0e9fcb78e0 | ||
|
|
19d5b931c4 | ||
|
|
18b7dee2f6 | ||
|
|
731b3e4b47 | ||
|
|
cdb4d29676 | ||
|
|
256f5a4a03 | ||
|
|
b69cb9b0f8 | ||
|
|
140a8f6efe | ||
|
|
14b99826eb | ||
|
|
0201d963b7 | ||
|
|
9be0fe6626 | ||
|
|
24b5dd033a | ||
|
|
686a6da019 | ||
|
|
bb0c8e7731 | ||
|
|
d3f7a04411 | ||
|
|
96bb1cbcae | ||
|
|
93fc562c44 | ||
|
|
c59f6b5fca | ||
|
|
ba7b49c35b | ||
|
|
1db3e0ad2f | ||
|
|
150b57d538 | ||
|
|
ce2f0b6a6c | ||
|
|
c3e6841293 | ||
|
|
12a53d647e | ||
|
|
314e37c433 | ||
|
|
05ed36240b | ||
|
|
520059d866 | ||
|
|
162a5b4acc | ||
|
|
d47fcbf2be | ||
|
|
cb07b92543 | ||
|
|
9274dbd49f | ||
|
|
d6cdd670b1 | ||
|
|
a5201a62e0 | ||
|
|
ee4c273d73 | ||
|
|
eea2108ac2 | ||
|
|
7f0e34d318 | ||
|
|
d6b7c0e9cb | ||
|
|
f049e208b6 | ||
|
|
88bb817a62 | ||
|
|
8ddee212e5 | ||
|
|
d3299166bb | ||
|
|
31d4be8015 | ||
|
|
45be3125de | ||
|
|
063b02e996 | ||
|
|
3f7b7847a5 | ||
|
|
7d87ad387b | ||
|
|
9dce341bc8 | ||
|
|
88eecd7671 | ||
|
|
83c412b33d | ||
|
|
31b65ade5b | ||
|
|
7cf7d33b6d | ||
|
|
4c4e415975 | ||
|
|
c55e3e0cbc | ||
|
|
240fd8586e | ||
|
|
ce23ab38f7 | ||
|
|
55696bfb31 | ||
|
|
400224a089 | ||
|
|
5c1823d6d2 | ||
|
|
a3642caa3d | ||
|
|
e2153435dc | ||
|
|
f307ecb380 | ||
|
|
50b823e76d | ||
|
|
84bda90065 | ||
|
|
d6fdcef4d4 | ||
|
|
62023acd4d | ||
|
|
75ef597ece | ||
|
|
0cbbbf260a | ||
|
|
4f99d5ce61 | ||
|
|
f96c4ccd64 | ||
|
|
ffa07b7df4 | ||
|
|
e81dd9a7ec | ||
|
|
30f2df3004 | ||
|
|
ac16fc1f0e | ||
|
|
e4aca3b31e | ||
|
|
212753f311 | ||
|
|
ccfcb58ed1 | ||
|
|
aa3df74fc1 | ||
|
|
f9d2c0d2d4 | ||
|
|
c066b2c82a | ||
|
|
ab795ec9e0 | ||
|
|
7042307955 | ||
|
|
f37d5fda3c | ||
|
|
c5b92eabe1 | ||
|
|
01453bce5f | ||
|
|
bd86ba36e2 | ||
|
|
88ab8db88d | ||
|
|
11406034c9 | ||
|
|
eef8671912 | ||
|
|
72383b1634 | ||
|
|
2d70be0ae2 | ||
|
|
5350738c22 | ||
|
|
0d29dd00fd | ||
|
|
d441481930 | ||
|
|
7ff9b854b6 | ||
|
|
46be24f6a3 | ||
|
|
3908e1601a | ||
|
|
1ef0a7ed82 | ||
|
|
43f5b4a078 | ||
|
|
83355a25c8 | ||
|
|
ec56bc32f8 | ||
|
|
050c3d2515 | ||
|
|
b649c6b301 | ||
|
|
99c9acccb0 | ||
|
|
524e6da591 | ||
|
|
6292bfd9cb | ||
|
|
83063d3a18 | ||
|
|
7b0dacabdd | ||
|
|
c8b861f0b6 | ||
|
|
09912509cb | ||
|
|
3e0940da8c | ||
|
|
d20e623f0c | ||
|
|
ef7a3ff1ed | ||
|
|
2115cb9085 | ||
|
|
5f981509ac | ||
|
|
c9733b944a | ||
|
|
f6f08eef61 | ||
|
|
67f469acfd | ||
|
|
d6328e7b58 | ||
|
|
46e08abad1 | ||
|
|
f6f4dad0ec | ||
|
|
077bfc0b6a | ||
|
|
c6a1f8f54f | ||
|
|
4038346de0 | ||
|
|
14d0b6b7ee | ||
|
|
d32ee1110b | ||
|
|
b0a1685dda | ||
|
|
a9540c5750 | ||
|
|
9a834e04ab | ||
|
|
8c9e881922 | ||
|
|
16e8bbf811 | ||
|
|
7b53f64249 | ||
|
|
4505fe77c4 | ||
|
|
37b059aa82 | ||
|
|
70cb9ca6a2 | ||
|
|
b812afabbc | ||
|
|
62eabb2944 | ||
|
|
fdc0b5b15a | ||
|
|
62e191abfc | ||
|
|
331c614330 | ||
|
|
6da7fa6786 | ||
|
|
c8d35b2ee6 | ||
|
|
ba73af8ced | ||
|
|
185d603a94 | ||
|
|
9804cf9a6d | ||
|
|
a4ea322c75 | ||
|
|
7e5cd85b8a | ||
|
|
8c665fb568 | ||
|
|
7d8fa18201 | ||
|
|
b413ca9a0b | ||
|
|
dfd8d5929c | ||
|
|
1b5319fde1 | ||
|
|
ff38dd086e | ||
|
|
7b02bc9ab4 | ||
|
|
380ca29f2c | ||
|
|
7389d26520 | ||
|
|
298ea082c0 | ||
|
|
ffefe4eb16 | ||
|
|
257e35198d | ||
|
|
38569c2f9f | ||
|
|
66d9c17985 | ||
|
|
03e115b65b | ||
|
|
804e6313b3 | ||
|
|
ed5cf597f9 | ||
|
|
4deb54b3a3 | ||
|
|
fe4cb9c47c | ||
|
|
1a9a555bee | ||
|
|
d7b0998af8 | ||
|
|
8aaf19479f | ||
|
|
069d119d03 | ||
|
|
a842d68038 | ||
|
|
e5cd656c6e | ||
|
|
ed4c1dcf07 | ||
|
|
b27b38d32b | ||
|
|
0cea78b3b1 | ||
|
|
efb59230e7 | ||
|
|
9d91da6656 | ||
|
|
a80df670be | ||
|
|
c8b19d507a | ||
|
|
da00aa4826 | ||
|
|
e213ad183e | ||
|
|
2ba7138881 | ||
|
|
1dd14e0bfd | ||
|
|
4631168d1d | ||
|
|
50ffca3bdc | ||
|
|
19c987585a | ||
|
|
2dcbcd0949 | ||
|
|
e46437d967 | ||
|
|
82fe053c7d | ||
|
|
757614eb57 | ||
|
|
08f97e9e29 | ||
|
|
0fbdf0ef1b | ||
|
|
73d798015d | ||
|
|
85e2912d62 | ||
|
|
004bf6f10b | ||
|
|
bbf23df6ab | ||
|
|
0a560d5f7b | ||
|
|
b6a03fff42 | ||
|
|
ae6b997b72 | ||
|
|
96d426a2e1 | ||
|
|
e78248c296 | ||
|
|
8212a9453a | ||
|
|
704ad94cb9 | ||
|
|
68bb9c3068 | ||
|
|
ed50aef6f7 | ||
|
|
1b4a6ff0cf | ||
|
|
99fa464dca | ||
|
|
fd41175ad8 | ||
|
|
fc93f550e0 | ||
|
|
f3818181d4 | ||
|
|
727ca278ca | ||
|
|
90f21d35cf | ||
|
|
0d0a8c013f | ||
|
|
7f24fe7d01 | ||
|
|
301c325af7 | ||
|
|
29d1332178 | ||
|
|
23fdc4d8f2 | ||
|
|
cf30dfb48c | ||
|
|
ec32030afc | ||
|
|
553e1dd4ee | ||
|
|
cdaea4142b | ||
|
|
5426022273 | ||
|
|
15a217eca4 | ||
|
|
ed031baf92 | ||
|
|
65a0221557 | ||
|
|
b2e9b8aecc | ||
|
|
d37010c6ae | ||
|
|
45c313824e | ||
|
|
7058d0c0b2 | ||
|
|
593bb9d9c2 | ||
|
|
db72e6f055 | ||
|
|
8fd69b4b50 | ||
|
|
ca721c128c | ||
|
|
b09a642f66 | ||
|
|
21ee86ffa0 | ||
|
|
02396641f6 | ||
|
|
a583281edb | ||
|
|
8b950a8031 | ||
|
|
a1355977d9 | ||
|
|
f3bfd01f93 | ||
|
|
143b1d436b | ||
|
|
3f11f4715c | ||
|
|
9af68cfcd7 | ||
|
|
32a177f5aa | ||
|
|
6c448b3060 | ||
|
|
4eaefc2c73 | ||
|
|
1673531b43 | ||
|
|
5112c7b0ef | ||
|
|
88b73f663f | ||
|
|
8bbf973097 | ||
|
|
14a45c9136 | ||
|
|
d2dec1be06 | ||
|
|
317065f986 | ||
|
|
24b56ac0e7 | ||
|
|
b46b98c5a7 | ||
|
|
3655ef8a7e | ||
|
|
3cf1c33b69 | ||
|
|
61caa47745 | ||
|
|
797d9763dd | ||
|
|
d2953d98f9 | ||
|
|
93d0bda627 | ||
|
|
5b956f8d95 | ||
|
|
6a10051d78 | ||
|
|
c3c5f11e9b | ||
|
|
790f97e501 | ||
|
|
cc22ffe6df | ||
|
|
72622f3dfb | ||
|
|
bea23820ab | ||
|
|
6d4a45797e | ||
|
|
dec46cd5ab | ||
|
|
98a6dd8a2c | ||
|
|
2de9f91671 | ||
|
|
e49fb46be7 | ||
|
|
8e0924a98c | ||
|
|
1781dd5b34 | ||
|
|
2a8bd44a06 | ||
|
|
f1799f86b1 | ||
|
|
602a673ca1 | ||
|
|
aca33c2a93 | ||
|
|
d08ed961bc | ||
|
|
bd41b5c54e | ||
|
|
22ade84ece | ||
|
|
8cfcf28b4e | ||
|
|
5bdf86c157 | ||
|
|
2a3a4635bd | ||
|
|
d3225fd5a8 | ||
|
|
326150f5cf | ||
|
|
ddc2eb327a | ||
|
|
8444b18c2c | ||
|
|
90c680414c | ||
|
|
2508c37ef2 | ||
|
|
52b9fcd814 | ||
|
|
70e17b9024 | ||
|
|
feefc51d6f | ||
|
|
ec5dfc4df2 | ||
|
|
bfe49d7a79 | ||
|
|
a2c6bd7f87 | ||
|
|
a6fb27d508 | ||
|
|
0d3f26a5ff | ||
|
|
e5da0c2fc5 | ||
|
|
70618aec1d | ||
|
|
199007f378 | ||
|
|
2d445828cb | ||
|
|
7cdb9ca5a1 | ||
|
|
093d04b7ca | ||
|
|
46bc9637fc | ||
|
|
cb402ac942 | ||
|
|
48023620e6 | ||
|
|
0bd2c12c13 | ||
|
|
72f55fe6ce | ||
|
|
8fe096d873 | ||
|
|
7ed303785c | ||
|
|
23ff41c974 | ||
|
|
4287303575 | ||
|
|
aa06ae534b | ||
|
|
c93b7fb2fd | ||
|
|
0b3464aa9e | ||
|
|
7d3250306a | ||
|
|
8230356d27 | ||
|
|
edd9ce4afd | ||
|
|
8e0ca92161 | ||
|
|
c51bf0eab0 | ||
|
|
b6be12969b | ||
|
|
a15328093a | ||
|
|
8db9f4447f | ||
|
|
4b9a450be3 | ||
|
|
659c227e6a | ||
|
|
01dec0a0ec | ||
|
|
2fdca72f02 | ||
|
|
e1d3b22c51 | ||
|
|
b822196e66 | ||
|
|
fa294f0a51 | ||
|
|
88c7a5ead9 | ||
|
|
557cea719d | ||
|
|
cbe51a2849 | ||
|
|
2f88c22fda | ||
|
|
29b93c933e | ||
|
|
306951837c | ||
|
|
6653c7965a | ||
|
|
b587147c81 | ||
|
|
866b3008cc | ||
|
|
57a7eb480d | ||
|
|
c997183fca | ||
|
|
661989d85d | ||
|
|
62378a9352 | ||
|
|
b4fefe483e | ||
|
|
31d8c2603e | ||
|
|
7977cb89aa | ||
|
|
bd8d6ccf63 | ||
|
|
2c4f9c6967 | ||
|
|
b03b057f66 | ||
|
|
31b7f4c569 | ||
|
|
1a27e95c1a | ||
|
|
044395500f | ||
|
|
bc7875bc8e | ||
|
|
0403be4516 | ||
|
|
f91a386566 | ||
|
|
f85482214e | ||
|
|
daab917ed1 | ||
|
|
8acfd1f19a | ||
|
|
32b1c0438b | ||
|
|
83279b536f | ||
|
|
9f382e4960 | ||
|
|
6c2a9f716a | ||
|
|
85efc068c4 | ||
|
|
c4b797f0b4 | ||
|
|
0a0cecf178 | ||
|
|
2d03586ae0 | ||
|
|
21ee6265d3 | ||
|
|
9792c6a08b | ||
|
|
eef01ee32a | ||
|
|
994a6e1749 | ||
|
|
fa4b17d9e9 | ||
|
|
4d0d1420df | ||
|
|
9a868fd465 | ||
|
|
d3b8062b6e | ||
|
|
dab76c86f3 | ||
|
|
4379f0c844 | ||
|
|
36c1546098 | ||
|
|
5fc110a1b2 | ||
|
|
9c73dd8d51 | ||
|
|
06a986d01c | ||
|
|
a002acb2b2 | ||
|
|
1a26827a5f | ||
|
|
4104ed9fa2 | ||
|
|
ff2d95aad0 | ||
|
|
66039028a5 | ||
|
|
fccee296a5 | ||
|
|
23f4f9f4f2 | ||
|
|
d1841a057d | ||
|
|
671befe610 | ||
|
|
8248bb0846 | ||
|
|
4431546a3a | ||
|
|
83d21bad1a | ||
|
|
5466457a06 | ||
|
|
2c7b8b525a | ||
|
|
1116957c19 | ||
|
|
5ec54d3cc2 | ||
|
|
c2d7000edf | ||
|
|
91d7074a07 | ||
|
|
d0bc5ebbd0 | ||
|
|
1ce3a6d65c | ||
|
|
75a28da8d9 | ||
|
|
0f4a7405db | ||
|
|
dd52f379bf | ||
|
|
cc2ec9e869 | ||
|
|
feeb46b873 | ||
|
|
0cdebc5b40 | ||
|
|
8cf7b55747 | ||
|
|
89b0a5a024 | ||
|
|
75291eac42 | ||
|
|
88185c70e0 | ||
|
|
3090fe62fe | ||
|
|
6388718b87 | ||
|
|
12eaa9f659 | ||
|
|
ac9382d12e | ||
|
|
ba30d1e7ce | ||
|
|
c58eb9314e | ||
|
|
53cd202a93 | ||
|
|
76a5b23e96 | ||
|
|
f42e37489b | ||
|
|
218c77dafb | ||
|
|
bbe109e4e5 | ||
|
|
bc47f79378 | ||
|
|
dcf82e7603 | ||
|
|
6eff157686 | ||
|
|
a358f772bd | ||
|
|
a21546028a | ||
|
|
02564cadfa | ||
|
|
739b359de2 | ||
|
|
0f400d72cd | ||
|
|
0f1ae237a0 | ||
|
|
0f6bb52776 | ||
|
|
946c0098a9 | ||
|
|
24b91b0d62 | ||
|
|
0fcb917a8d | ||
|
|
0f9e45dcb1 | ||
|
|
b55eacad78 | ||
|
|
6d7f95186d | ||
|
|
51a5162e66 | ||
|
|
2cf9915208 | ||
|
|
c7f652ace5 | ||
|
|
697f25700b | ||
|
|
dea1d73943 | ||
|
|
027eab1368 | ||
|
|
28ced406bc | ||
|
|
fff658d551 | ||
|
|
4a6e907f3f | ||
|
|
da58b97096 | ||
|
|
a0cae4a6a4 | ||
|
|
ccb0fb3e97 | ||
|
|
69e86c239b | ||
|
|
00f32ce9c2 | ||
|
|
8c4ce5e3c0 | ||
|
|
3b53de1b33 | ||
|
|
182e40800e | ||
|
|
5c5ef561cf | ||
|
|
4674328303 | ||
|
|
6e8a51b21a | ||
|
|
9e552aa87d | ||
|
|
47dc79683f | ||
|
|
7af347b6be | ||
|
|
7720d9b96f | ||
|
|
fb87c9f7c3 | ||
|
|
ca132c2668 | ||
|
|
71646ccfb0 | ||
|
|
1f44fc9dfd | ||
|
|
0c2b70d3f9 | ||
|
|
652bcb9eef | ||
|
|
8c5a5fd6b2 | ||
|
|
1e9ecf656f | ||
|
|
21117da114 | ||
|
|
67043a76e5 | ||
|
|
9f05059800 | ||
|
|
05ac6cd756 | ||
|
|
bc8083fd6b | ||
|
|
af05604e99 | ||
|
|
e22967ada8 | ||
|
|
247d78374c | ||
|
|
0b3a7d5c95 |
@@ -15,6 +15,7 @@ docker-compose.yml
|
||||
.env.*
|
||||
*.log
|
||||
.obsigate-backup
|
||||
backend/VERSION
|
||||
.pytest_cache
|
||||
htmlcov
|
||||
.coverage
|
||||
|
||||
+20
-2
@@ -16,8 +16,17 @@ OBSIGATE_ADMIN_PASSWORD=chab30
|
||||
|
||||
# Rate limiting
|
||||
# OBSIGATE_LOGIN_MAX_ATTEMPTS=10
|
||||
# OBSIGATE_ACCOUNT_MAX_ATTEMPTS=10
|
||||
# OBSIGATE_LOGIN_WINDOW_SECONDS=900
|
||||
|
||||
# IP client derrière un reverse proxy (fait confiance à X-Forwarded-For)
|
||||
# OBSIGATE_TRUST_PROXY=false
|
||||
|
||||
# Webhooks : sécurité SSRF
|
||||
# OBSIGATE_WEBHOOK_ALLOW_HTTP=false # autoriser http:// (défaut : HTTPS requis)
|
||||
# OBSIGATE_WEBHOOK_ALLOW_PRIVATE=false # autoriser les IP privées/boucle
|
||||
# Secret d'un webhook : OBSIGATE_WEBHOOK_SECRET_<ID_WEBHOOK_EN_MAJUSCULES>
|
||||
|
||||
# Watcher
|
||||
# OBSIGATE_WATCHER_ENABLED=true
|
||||
# OBSIGATE_WATCHER_USE_POLLING=false
|
||||
@@ -33,14 +42,23 @@ OBSIGATE_ADMIN_PASSWORD=chab30
|
||||
# Backup
|
||||
# OBSIGATE_BACKUP_DIR=.obsigate-backup
|
||||
|
||||
# PDF (ROADMAP #74)
|
||||
# OBSIGATE_PDF_MAX_SIZE_MB=50 # PDFs plus volumineux = texte non indexé
|
||||
# OBSIGATE_PDF_EXTRACT_TIMEOUT=30 # secondes avant abandon de l'extraction
|
||||
|
||||
# WebAuthn / MFA (ROADMAP #64) — nécessaire hors localhost
|
||||
# OBSIGATE_WEBAUTHN_RP_ID=obsigate.example.com
|
||||
# OBSIGATE_WEBAUTHN_RP_NAME=ObsiGate
|
||||
# OBSIGATE_WEBAUTHN_ORIGINS=https://obsigate.example.com
|
||||
|
||||
# ── AI Provider Configuration ──
|
||||
# Définir au moins un provider pour activer les fonctionnalités AI dans l'éditeur
|
||||
|
||||
# AI_DEFAULT_PROVIDER=deepseek # deepseek | openrouter | gemini
|
||||
|
||||
# DeepSeek (recommandé, bon marché)
|
||||
DEEPSEEK_API_KEY=sk-87d93019602e4c279679dfe80d504bc3
|
||||
DEEPSEEK_MODEL=deepseek-v4-pro
|
||||
DEEPSEEK_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
DEEPSEEK_MODEL=deepseek-chat
|
||||
|
||||
# OpenRouter (accès à plusieurs modèles)
|
||||
# OPENROUTER_API_KEY=sk-or-v1-...
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# Binary files — prevent CRLF corruption on Windows
|
||||
*.png binary
|
||||
*.ico binary
|
||||
*.icns binary
|
||||
*.jpg binary
|
||||
*.jpeg binary
|
||||
*.gif binary
|
||||
*.webp binary
|
||||
*.zip binary
|
||||
*.exe binary
|
||||
*.msi binary
|
||||
*.AppImage binary
|
||||
*.deb binary
|
||||
+135
-4
@@ -30,7 +30,7 @@ jobs:
|
||||
run: ruff check backend/
|
||||
|
||||
- name: Mypy (type checker)
|
||||
run: mypy backend/ --ignore-missing-imports || echo "mypy found type errors (advisory — 28 pre-existing issues)"
|
||||
run: mypy backend/ --ignore-missing-imports
|
||||
|
||||
- name: Frontend validation
|
||||
run: node tests/frontend/validate-imports.mjs
|
||||
@@ -38,6 +38,37 @@ jobs:
|
||||
- name: Frontend unit tests
|
||||
run: node tests/frontend/unit.test.mjs
|
||||
|
||||
- name: Frontend JSDOM tests (PaneManager + Excalidraw + Plugins + AI + SW + Collab + Mobile + Semantic + Desktop + Inline edition)
|
||||
run: |
|
||||
cd tests/frontend
|
||||
if [ -d node_modules ]; then
|
||||
node pane-manager.test.mjs
|
||||
node excalidraw-viewer.test.mjs
|
||||
node plugins.test.mjs
|
||||
node ai.test.mjs
|
||||
node sw.test.mjs
|
||||
node collab.test.mjs
|
||||
node mobile-editor.test.mjs
|
||||
node semantic-search.test.mjs
|
||||
node desktop.test.mjs
|
||||
node toolbar-order.test.mjs
|
||||
node editor-inline.test.mjs
|
||||
else
|
||||
echo "tests/frontend/node_modules missing - installing jsdom"
|
||||
npm install --no-audit --no-fund --silent
|
||||
node pane-manager.test.mjs
|
||||
node excalidraw-viewer.test.mjs
|
||||
node plugins.test.mjs
|
||||
node ai.test.mjs
|
||||
node sw.test.mjs
|
||||
node collab.test.mjs
|
||||
node mobile-editor.test.mjs
|
||||
node semantic-search.test.mjs
|
||||
node desktop.test.mjs
|
||||
node toolbar-order.test.mjs
|
||||
node editor-inline.test.mjs
|
||||
fi
|
||||
|
||||
# ── Tests ─────────────────────────────────────────────────────────
|
||||
test:
|
||||
needs: lint
|
||||
@@ -54,6 +85,7 @@ jobs:
|
||||
run: |
|
||||
pip install pytest pytest-cov pytest-asyncio httpx
|
||||
pip install -r backend/requirements.txt
|
||||
pip install -r backend/requirements-test.txt || echo "test deps install failed (non-blocking — PDF tests will skip)"
|
||||
|
||||
- name: Run tests
|
||||
run: pytest tests/ --cov=backend --cov-report=xml --cov-report=term -q
|
||||
@@ -82,7 +114,7 @@ jobs:
|
||||
pip install -r backend/requirements.txt
|
||||
|
||||
- name: Bandit (SAST)
|
||||
run: bandit -r backend/ -c pyproject.toml 2>/dev/null || bandit -r backend/ --skip B101
|
||||
run: bandit -r backend/ --skip B101,B110,B310 || echo "bandit found issues (non-blocking)"
|
||||
|
||||
- name: Pip-audit (dependency vulnerabilities)
|
||||
run: pip-audit || echo "pip-audit found vulnerabilities (non-blocking)"
|
||||
@@ -94,8 +126,107 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Build Docker image
|
||||
run: docker build -t obsigate:ci .
|
||||
- name: Version livrée
|
||||
# VERSION (racine du dépôt) est copié dans l'image par le Dockerfile :
|
||||
# plus aucun numéro généré ni codé en dur dans le pipeline.
|
||||
run: echo "Version livree = $(cat VERSION)"
|
||||
|
||||
- name: Configure DNS (workaround flaky 127.0.0.11 resolver)
|
||||
# GitHub Actions runners occasionally fail to resolve auth.docker.io via
|
||||
# the embedded Docker DNS (127.0.0.11:53 → "server misbehaving").
|
||||
# Force the daemon to use public DNS as a fallback.
|
||||
run: |
|
||||
sudo mkdir -p /etc/docker
|
||||
if ! grep -q "dns" /etc/docker/daemon.json 2>/dev/null; then
|
||||
echo '{"dns": ["8.8.8.8", "1.1.1.1", "9.9.9.9"]}' | sudo tee /etc/docker/daemon.json
|
||||
sudo systemctl restart docker || sudo service docker restart || true
|
||||
sleep 3
|
||||
fi
|
||||
|
||||
- name: Build Docker image (retry on transient DNS/network errors)
|
||||
run: |
|
||||
for attempt in 1 2 3; do
|
||||
echo "=== docker build attempt $attempt/3 ==="
|
||||
if docker build -t obsigate:ci . ; then
|
||||
echo "✓ Docker build succeeded"
|
||||
exit 0
|
||||
fi
|
||||
echo "✗ Build failed (attempt $attempt)"
|
||||
if [ $attempt -lt 3 ]; then
|
||||
sleep $((attempt * 10))
|
||||
fi
|
||||
done
|
||||
echo "✗ Docker build failed after 3 attempts"
|
||||
exit 1
|
||||
|
||||
- name: Verify image
|
||||
run: docker images obsigate:ci
|
||||
|
||||
# ── E2E Tests (Playwright) ─────────────────────────────────────────
|
||||
e2e:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "20"
|
||||
|
||||
- name: Install Playwright
|
||||
run: |
|
||||
npm ci
|
||||
npx playwright install --with-deps chromium
|
||||
|
||||
- name: Start ObsiGate
|
||||
run: |
|
||||
docker rm -f obsigate-e2e 2>/dev/null || true
|
||||
docker create --name obsigate-e2e -p 2029:8080 \
|
||||
-v $(pwd)/test_vault:/vaults/TestVault \
|
||||
-v $(pwd)/test_dir:/vaults/TestDir \
|
||||
-e VAULT_1_NAME=TestVault \
|
||||
-e VAULT_1_PATH=/vaults/TestVault \
|
||||
-e DIR_1_NAME=TestDir \
|
||||
-e DIR_1_PATH=/vaults/TestDir \
|
||||
-e OBSIGATE_AUTH_ENABLED=false \
|
||||
obsigate:ci
|
||||
# Docker-in-docker : le bind mount $(pwd)/... pointe sur un chemin
|
||||
# du job container, inexistant sur l'hôte → montage vide. Les -v
|
||||
# créent quand même /vaults/* dans le container ; on y copie les
|
||||
# fixtures avant le démarrage (l'indexeur scanne au démarrage).
|
||||
docker cp test_vault/. obsigate-e2e:/vaults/TestVault
|
||||
docker cp test_dir/. obsigate-e2e:/vaults/TestDir
|
||||
docker start obsigate-e2e
|
||||
# Le port publié est joignable via l'IP de la passerelle (localhost
|
||||
# du job container ne voit pas le port publié sur l'hôte).
|
||||
GW=$(ip route show default | awk '{print $3}' || echo 172.17.0.1)
|
||||
echo "Gateway IP: $GW"
|
||||
# Wait for health check
|
||||
for i in $(seq 1 30); do
|
||||
if curl -sf "http://$GW:2029/api/health"; then echo "Health OK"; break; fi
|
||||
sleep 1
|
||||
done
|
||||
curl -sf "http://$GW:2029/api/health" >/dev/null || { echo "App not reachable at $GW:2029"; exit 1; }
|
||||
# Les fixtures sont copiées via `docker cp` en root → rendre le vault
|
||||
# inscriptible par l'utilisateur non-root de l'app (UID 1000), sinon
|
||||
# toute création/édition de fichier renvoie 403 « Vault is read-only ».
|
||||
docker exec -u 0 obsigate-e2e chmod -R a+rwX /vaults/TestVault /vaults/TestDir || true
|
||||
|
||||
- name: Run E2E tests
|
||||
run: |
|
||||
GW=$(ip route show default | awk '{print $3}' || echo 172.17.0.1)
|
||||
echo "Using BASE_URL=http://$GW:2029"
|
||||
BASE_URL="http://$GW:2029" npx playwright test --project=chromium-desktop --reporter=list
|
||||
|
||||
- name: Upload test results
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: playwright-report
|
||||
path: playwright-report/
|
||||
retention-days: 7
|
||||
|
||||
- name: Cleanup
|
||||
if: always()
|
||||
run: docker rm -f obsigate-e2e
|
||||
@@ -0,0 +1,131 @@
|
||||
name: Desktop Build
|
||||
|
||||
on:
|
||||
# Lancement manuel uniquement : aucun runner self-hosted [windows/linux, desktop]
|
||||
# n'est enregistré dans Gitea. Le build Windows se fait en local
|
||||
# (desktop/build-windows.bat). Ce workflow reste disponible pour un futur runner.
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-windows:
|
||||
runs-on: [self-hosted, windows, desktop] # runner sur ta machine Windows
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Rust
|
||||
run: rustup default stable && rustup update
|
||||
|
||||
- name: Setup Tauri CLI
|
||||
run: cargo install tauri-cli --version "^2.0"
|
||||
|
||||
- name: Setup Python 3.11 embed
|
||||
run: |
|
||||
curl -L -o python-embed.zip "https://www.python.org/ftp/python/3.11.15/python-3.11.15-embed-amd64.zip"
|
||||
powershell Expand-Archive -Path python-embed.zip -DestinationPath desktop/python-embed
|
||||
echo import site >> desktop/python-embed/python311._pth
|
||||
cd desktop/python-embed
|
||||
./python.exe -m pip install --no-cache-dir -r ../../backend/requirements.txt
|
||||
|
||||
- name: Stage backend + frontend for bundle
|
||||
working-directory: desktop
|
||||
run: |
|
||||
xcopy /E /I /Q /Y "..\backend" "backend"
|
||||
xcopy /E /I /Q /Y "..\frontend" "frontend"
|
||||
|
||||
- name: Build MSI
|
||||
working-directory: desktop
|
||||
shell: powershell
|
||||
env:
|
||||
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
|
||||
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
|
||||
run: |
|
||||
if ([string]::IsNullOrEmpty($env:TAURI_SIGNING_PRIVATE_KEY)) {
|
||||
Write-Host "TAURI_SIGNING_PRIVATE_KEY absent - build sans artefacts de mise a jour."
|
||||
cargo tauri build --bundles msi --config '{"bundle":{"createUpdaterArtifacts":false}}'
|
||||
} else {
|
||||
Write-Host "Signature des artefacts de mise a jour activee."
|
||||
cargo tauri build --bundles msi
|
||||
}
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
|
||||
- name: Upload MSI artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: obsigate-windows-msi
|
||||
path: |
|
||||
desktop/target/release/bundle/msi/*.msi
|
||||
desktop/target/release/bundle/msi/*.msi.sig
|
||||
retention-days: 30
|
||||
|
||||
- name: Publish to Gitea Release
|
||||
if: startsWith(github.ref, 'refs/tags/v')
|
||||
run: |
|
||||
$MSI = Get-ChildItem desktop/target/release/bundle/msi/*.msi | Select-Object -First 1
|
||||
echo "MSI ready: $MSI"
|
||||
|
||||
build-linux:
|
||||
runs-on: [self-hosted, linux, desktop] # runner sur une machine Linux
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Rust
|
||||
run: rustup default stable && rustup update
|
||||
|
||||
- name: Install system deps
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev
|
||||
|
||||
- name: Setup Tauri CLI
|
||||
run: cargo install tauri-cli --version "^2.0"
|
||||
|
||||
- name: Setup Python venv
|
||||
run: |
|
||||
python3 -m venv desktop/python-embed/venv
|
||||
source desktop/python-embed/venv/bin/activate
|
||||
pip install -r backend/requirements.txt
|
||||
|
||||
- name: Stage backend + frontend for bundle
|
||||
working-directory: desktop
|
||||
run: |
|
||||
cp -r ../backend backend
|
||||
cp -r ../frontend frontend
|
||||
|
||||
- name: Build AppImage
|
||||
working-directory: desktop
|
||||
env:
|
||||
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
|
||||
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
|
||||
run: |
|
||||
if [ -z "$TAURI_SIGNING_PRIVATE_KEY" ]; then
|
||||
echo "TAURI_SIGNING_PRIVATE_KEY absent - build sans artefacts de mise a jour."
|
||||
cargo tauri build --bundles appimage --config '{"bundle":{"createUpdaterArtifacts":false}}'
|
||||
else
|
||||
echo "Signature des artefacts de mise a jour activee."
|
||||
cargo tauri build --bundles appimage
|
||||
fi
|
||||
|
||||
- name: Build deb
|
||||
working-directory: desktop
|
||||
env:
|
||||
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
|
||||
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
|
||||
run: |
|
||||
if [ -z "$TAURI_SIGNING_PRIVATE_KEY" ]; then
|
||||
echo "TAURI_SIGNING_PRIVATE_KEY absent - build sans artefacts de mise a jour."
|
||||
cargo tauri build --bundles deb --config '{"bundle":{"createUpdaterArtifacts":false}}'
|
||||
else
|
||||
echo "Signature des artefacts de mise a jour activee."
|
||||
cargo tauri build --bundles deb
|
||||
fi
|
||||
|
||||
- name: Upload Linux artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: obsigate-linux
|
||||
path: |
|
||||
desktop/target/release/bundle/appimage/*.AppImage
|
||||
desktop/target/release/bundle/appimage/*.AppImage.sig
|
||||
desktop/target/release/bundle/deb/*.deb
|
||||
desktop/target/release/bundle/deb/*.deb.sig
|
||||
retention-days: 30
|
||||
@@ -0,0 +1,68 @@
|
||||
#!/bin/sh
|
||||
# -----------------------------------------------------------------------------
|
||||
# ObsiGate — rattache l'incrément de version au commit qui vient d'être créé,
|
||||
# puis crée le tag `vX.Y.Z` de la version livrée.
|
||||
#
|
||||
# Deux situations :
|
||||
# 1. `prepare-commit-msg` a incrémenté ./VERSION et mis à jour les fichiers
|
||||
# dérivés (package.json, desktop, READMEs, ROADMAP, CHANGELOG) : on les
|
||||
# rattache ici au commit via `--amend` (le commit n'est pas encore poussé) ;
|
||||
# 2. le message venait d'un éditeur : l'incrément a été différé, il est
|
||||
# calculé ici à partir du message final (`COMMIT_EDITMSG`).
|
||||
#
|
||||
# Résultat : la version livrée, le contenu du commit et le tag correspondent
|
||||
# toujours. Le tag est publié avec la branche à chaque push (`push.followTags`,
|
||||
# posé par scripts/install-hooks.sh).
|
||||
# -----------------------------------------------------------------------------
|
||||
set -eu
|
||||
|
||||
[ "${SKIP_VERSION_BUMP:-}" = "" ] || exit 0
|
||||
[ "${OBSIGATE_VERSION_AMENDING:-}" = "" ] || exit 0 # garde anti-récursion
|
||||
|
||||
root="$(git rev-parse --show-toplevel)"
|
||||
version_file="$root/VERSION"
|
||||
[ -f "$version_file" ] || exit 0
|
||||
|
||||
# Opérations en cours (rebase, merge, cherry-pick…) : ne jamais amender ici.
|
||||
for state in rebase-merge rebase-apply CHERRY_PICK_HEAD REVERT_HEAD MERGE_HEAD BISECT_LOG; do
|
||||
if [ -e "$(git rev-parse --git-path "$state")" ]; then
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
list="$(git rev-parse --git-path obsigate-version-files)"
|
||||
defer="$(git rev-parse --git-path obsigate-version-defer)"
|
||||
|
||||
# Cas 2 : l'incrément avait été différé (commit rédigé dans un éditeur).
|
||||
if [ -f "$defer" ]; then
|
||||
rm -f "$defer"
|
||||
rm -f "$list"
|
||||
tool="$root/scripts/bump_version.py"
|
||||
py=""
|
||||
for cand in python3 python; do
|
||||
if command -v "$cand" >/dev/null 2>&1; then py="$cand"; break; fi
|
||||
done
|
||||
if [ -n "$py" ] && [ -f "$tool" ]; then
|
||||
"$py" "$tool" --from-message-file "$(git rev-parse --git-path COMMIT_EDITMSG)" \
|
||||
--files-out "$list" || true
|
||||
fi
|
||||
fi
|
||||
|
||||
# Rattachement des fichiers synchronisés au commit qui vient d'être créé.
|
||||
if [ -s "$list" ]; then
|
||||
# shellcheck disable=SC2046
|
||||
git add -- $(tr -d '\r' < "$list")
|
||||
OBSIGATE_VERSION_AMENDING=1 \
|
||||
git commit --amend --no-edit --no-verify --quiet
|
||||
rm -f "$list"
|
||||
fi
|
||||
|
||||
ver="$(tr -d ' \t\r\n' < "$version_file")"
|
||||
[ -n "$ver" ] || exit 0
|
||||
|
||||
if git rev-parse -q --verify "refs/tags/v$ver" >/dev/null 2>&1; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
git tag -a "v$ver" -m "ObsiGate v$ver"
|
||||
echo "ObsiGate version : tag v$ver créé (poussé avec la branche)."
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/bin/sh
|
||||
# -----------------------------------------------------------------------------
|
||||
# ObsiGate — incrément automatique de la version livrée (SemVer) à chaque commit.
|
||||
#
|
||||
# Source unique de vérité : ./VERSION (MAJEUR.MINEUR.CORRECTIF)
|
||||
#
|
||||
# `!:` / `BREAKING CHANGE:` -> MAJEUR (x.0.0)
|
||||
# `feat:` -> MINEUR (x.y.0)
|
||||
# tout le reste (fix, perf…) -> CORRECTIF (x.y.z)
|
||||
#
|
||||
# Aucun incrément pour : merge, squash, revert, `chore(release)`, amend.
|
||||
# Commit rédigé dans un éditeur (message pas encore saisi) : l'incrément est
|
||||
# différé à `post-commit`, qui dispose du message final.
|
||||
#
|
||||
# `scripts/bump_version.py` incrémente VERSION et resynchronise les fichiers
|
||||
# dérivés (package.json, desktop, READMEs, ROADMAP, CHANGELOG) ; le `git add` est
|
||||
# fait ici, par le hook lui-même. La liste des fichiers est laissée à
|
||||
# `.githooks/post-commit`, qui rattache l'incrément au commit (git fige l'arbre
|
||||
# avant `prepare-commit-msg` : un `git add` à cet instant ne serait pris en
|
||||
# compte qu'au commit suivant).
|
||||
#
|
||||
# Neutraliser ponctuellement : SKIP_VERSION_BUMP=1 git commit ...
|
||||
# -----------------------------------------------------------------------------
|
||||
set -eu
|
||||
|
||||
[ "${SKIP_VERSION_BUMP:-}" = "" ] || exit 0
|
||||
|
||||
msg_file="${1:-}"
|
||||
source_kind="${2:-message}"
|
||||
|
||||
case "$source_kind" in
|
||||
merge|squash|commit) exit 0 ;; # merge / squash / amend (-c, -C, --amend)
|
||||
esac
|
||||
|
||||
[ -n "$msg_file" ] && [ -f "$msg_file" ] || exit 0
|
||||
|
||||
root="$(git rev-parse --show-toplevel)"
|
||||
tool="$root/scripts/bump_version.py"
|
||||
[ -f "$tool" ] || exit 0
|
||||
|
||||
py=""
|
||||
for cand in python3 python; do
|
||||
if command -v "$cand" >/dev/null 2>&1; then py="$cand"; break; fi
|
||||
done
|
||||
if [ -z "$py" ]; then
|
||||
echo "⚠ version : aucun interpréteur Python trouvé — version non incrémentée." >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
list="$(git rev-parse --git-path obsigate-version-files)"
|
||||
defer="$(git rev-parse --git-path obsigate-version-defer)"
|
||||
|
||||
# Message pas encore rédigé (ouverture d'un éditeur) : on diffère l'incrément.
|
||||
if ! grep -qvE '^[[:space:]]*(#|$)' "$msg_file"; then
|
||||
: > "$defer"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
rm -f "$defer" # marqueur d'un commit précédent avorté
|
||||
|
||||
out=""
|
||||
if out="$("$py" "$tool" --from-message-file "$msg_file" --files-out "$list" 2>&1)"; then
|
||||
printf '%s\n' "$out"
|
||||
else
|
||||
status=$?
|
||||
if [ "$status" = "3" ]; then
|
||||
rm -f "$list"
|
||||
exit 0 # 3 = version inchangée (revert, release…) : pas une erreur
|
||||
fi
|
||||
echo "✗ version : échec de l'incrément automatique de ./VERSION" >&2
|
||||
printf '%s\n' "$out" >&2
|
||||
echo " → corriger, ou passer outre avec : SKIP_VERSION_BUMP=1 git commit ..." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ajout des fichiers synchronisés (chemins explicites).
|
||||
if [ -s "$list" ]; then
|
||||
# shellcheck disable=SC2046
|
||||
git add -- $(tr -d '\r' < "$list")
|
||||
fi
|
||||
+26
-1
@@ -1,14 +1,39 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
# Scripts de debogage temporaires (analyse de logs e2e)
|
||||
_*.py
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
test_vault/
|
||||
.venv/
|
||||
.venv-e2e/
|
||||
venv/
|
||||
*.egg-info/
|
||||
dist/
|
||||
build/
|
||||
config.json
|
||||
data/
|
||||
.coverage
|
||||
.coverage
|
||||
odysseus/
|
||||
node_modules/
|
||||
test-results/
|
||||
playwright-report/
|
||||
**/.obsigate-backup/
|
||||
|
||||
# Desktop build artifacts
|
||||
desktop/target/
|
||||
desktop/python-embed/
|
||||
desktop/frontend-static/
|
||||
desktop/checksums.txt
|
||||
desktop/backend/
|
||||
desktop/frontend/
|
||||
backend/VERSION
|
||||
|
||||
# Tauri updater signing keys (private key — never commit)
|
||||
desktop/*.key
|
||||
desktop/*.key.pub
|
||||
desktop/key/
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# AGENTS.md — Instructions obligatoires du dépôt ObsiGate
|
||||
|
||||
> Ces instructions s'appliquent à **toute** intervention (humaine ou IA) sur ce dépôt.
|
||||
|
||||
## Règle n°1 — Méthode de livraison unique
|
||||
|
||||
Avant toute tâche (fonctionnalité, bug, refactor), **lire et appliquer**
|
||||
[`docs/DELIVERY_WORKFLOW.md`](./docs/DELIVERY_WORKFLOW.md) (Definition of Done).
|
||||
Aucune tâche n'est terminée avant que sa checklist soit complète **et le CI vert**.
|
||||
|
||||
## Avant de commencer
|
||||
|
||||
1. Lire [`docs/ROADMAP.md`](./docs/ROADMAP.md) (travail à venir + index) et
|
||||
[`docs/ISSUES_TODOLIST.md`](./docs/ISSUES_TODOLIST.md) (bugs).
|
||||
2. Identifier ou créer l'**ID stable** (`#NN` pour une feature, `BUG-NNN` pour un bug)
|
||||
et passer son statut à « en cours » **avant** de coder.
|
||||
|
||||
## À la fin de chaque tâche (obligatoire)
|
||||
|
||||
- Ajouter/mettre à jour les **tests unitaires**.
|
||||
- Vérifications locales vertes : `pytest`, `ruff`, `mypy`, tests frontend (`E2E` si UI).
|
||||
- Mettre à jour la documentation requise : `CHANGELOG.md` (`[Unreleased]`), `docs/ROADMAP.md`
|
||||
(statut + index), fiche `docs/features/` **ou** `docs/archive/`, `docs/ISSUES_TODOLIST.md`,
|
||||
guide utilisateur i18n FR/EN + README si impact utilisateur.
|
||||
- **Commit** conventionnel référençant l'ID, puis **push**.
|
||||
- Version : le fichier VERSION (racine du dépôt) est la **source unique de
|
||||
vérité (MAJEUR.MINEUR.CORRECTIF), incrémenté automatiquement à chaque commit** par le hook
|
||||
.githooks/prepare-commit-msg — feat → mineur, !: / BREAKING CHANGE → majeur, sinon
|
||||
correctif. Le même commit resynchronise package.json, le desktop Tauri, README.md/
|
||||
README.fr.md, docs/ROADMAP.md et publie la section [Unreleased] du CHANGELOG.md en
|
||||
[X.Y.Z] — date ; le tag vX.Y.Z est créé au commit et publié au push (push.followTags).
|
||||
Hooks à installer une fois par clone : scripts/install-hooks.sh. Garde-fou :
|
||||
tests/test_version.py (détail : docs/DELIVERY_WORKFLOW.md §7).
|
||||
- Vérifier le **CI Gitea vert** (jobs `lint`, `test`, `security`, `build`, `e2e`).
|
||||
|
||||
## Cartographie documentaire
|
||||
|
||||
| Sujet | Fichier |
|
||||
|---|---|
|
||||
| Méthode de livraison / DoD | `docs/DELIVERY_WORKFLOW.md` |
|
||||
| Version livrée (source unique) | VERSION + scripts/bump_version.py |
|
||||
| Travail à venir + index | `docs/ROADMAP.md` |
|
||||
| Historique des versions | `CHANGELOG.md` |
|
||||
| Conception par feature | `docs/features/<slug>.md` |
|
||||
| Archive du complété | `docs/archive/COMPLETED_v1-v2.md` |
|
||||
| Bugs / TODO | `docs/ISSUES_TODOLIST.md` |
|
||||
| Build & releases | `docs/DEVELOPMENT_AND_RELEASES.md` |
|
||||
| Standards de code | `docs/CONTRIBUTING.md` |
|
||||
|
||||
## Conventions
|
||||
|
||||
- Commits : `type: description` — `feat`, `fix`, `perf`, `refactor`, `docs`, `style`, `chore`, `test`.
|
||||
- **Ne jamais** committer de secrets, clés ou tokens.
|
||||
- Réponses et documentation en **français** ; respecter le style du code existant.
|
||||
+1014
-1
File diff suppressed because it is too large
Load Diff
+8
-2
@@ -3,7 +3,8 @@
|
||||
FROM python:3.11-slim AS builder
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends gcc libffi-dev libc6-dev libpango-1.0-0 libpangocairo-1.0-0 shared-mime-info \
|
||||
&& apt-get install -y --no-install-recommends gcc libffi-dev libc6-dev \
|
||||
&& apt-get clean \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /build
|
||||
@@ -14,7 +15,6 @@ RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
|
||||
FROM python:3.11-slim
|
||||
|
||||
LABEL maintainer="Bruno Beloeil" \
|
||||
version="1.4.0" \
|
||||
description="ObsiGate — lightweight web interface for Obsidian vaults"
|
||||
|
||||
WORKDIR /app
|
||||
@@ -25,12 +25,18 @@ COPY --from=builder /install /usr/local
|
||||
# WeasyPrint runtime dependencies
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends libpango-1.0-0 libpangocairo-1.0-0 shared-mime-info \
|
||||
&& apt-get clean \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy application code
|
||||
COPY backend/ ./backend/
|
||||
COPY frontend/ ./frontend/
|
||||
|
||||
# Version livrée : `VERSION` (racine du dépôt) est la source unique de vérité —
|
||||
# copié dans l'image, jamais un numéro codé en dur. `backend/version.py` le lit
|
||||
# et /api/health l'expose (header + boîte À propos).
|
||||
COPY VERSION ./VERSION
|
||||
|
||||
# Create non-root user for security + data directory for auth persistence
|
||||
# Using explicit UID/GID 1000 to match common host user and docker-compose settings
|
||||
RUN groupadd -g 1000 obsigate && useradd -u 1000 -g obsigate -d /app -s /sbin/nologin obsigate \
|
||||
|
||||
+923
@@ -0,0 +1,923 @@
|
||||
# ObsiGate
|
||||
|
||||
> **Version française** — ce document est le miroir synchronisé de [README.md](README.md) (référence complète). Dernière synchronisation : juin 2026.
|
||||
|
||||
**Porte d'entrée web ultra-léger pour vos vaults Obsidian** — Accédez, naviguez et recherchez dans toutes vos notes Obsidian depuis n'importe quel appareil via une interface web moderne et responsive.
|
||||
|
||||
[]()
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://www.docker.com/)
|
||||
[](https://www.python.org/)
|
||||
[](https://git.dracodev.net/Projets/ObsiGate/actions)
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ [🔍 Recherche...] [☀/🌙 Thème] ObsiGate │
|
||||
├──────────────┬──────────────────────────────────────────┤
|
||||
│ SIDEBAR │ CONTENT AREA │
|
||||
│ ▼ Recettes │ 📄 Titre du fichier │
|
||||
│ 📁 Soupes │ Tags: #recette #rapide │
|
||||
│ 📄 Pizza │ [Contenu Markdown rendu] │
|
||||
│ ▼ IT │ │
|
||||
│ 📁 Docker │ │
|
||||
│ Tags Cloud │ │
|
||||
└──────────────┴──────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📋 Table des matières
|
||||
|
||||
- [Fonctionnalités](#fonctionnalites)
|
||||
- [Prérequis](#prerequis)
|
||||
- [Installation rapide](#installation-rapide)
|
||||
- [Configuration détaillée](#configuration-detaillee)
|
||||
- [Variables d'environnement](#variables-denvironnement)
|
||||
- [🔒 Authentification](#authentification)
|
||||
- [Ajouter une nouvelle vault](#ajouter-une-nouvelle-vault)
|
||||
- [Build & déploiement avec build.sh](#build-deploiement-avec-buildsh)
|
||||
- [Rendu d'images Obsidian](#rendu-dimages-obsidian)
|
||||
- [Desktop (Tauri) — Application native](#desktop-tauri-application-native)
|
||||
- [Utilisation](#utilisation)
|
||||
- [API](#api)
|
||||
- [Recherche avancée](#recherche-avancee)
|
||||
- [Dépannage](#depannage)
|
||||
- [Performance](#performance)
|
||||
- [Sécurité](#securite)
|
||||
- [Stack technique](#stack-technique)
|
||||
- [Architecture](#architecture)
|
||||
- [Développement](#developpement)
|
||||
- [Licence](#licence)
|
||||
- [Changelog](#changelog)
|
||||
|
||||
---
|
||||
|
||||
## ✨ Fonctionnalités
|
||||
|
||||
- **🤖 AI Editor intégré** — Éditeur CodeMirror 6 avec toolbar IA : amélioration, correction, traduction, génération, réécriture personnalisée, toolbox (liste, tableau, frontmatter, canvas) — multi-provider DeepSeek/OpenRouter/Gemini
|
||||
- **🧩 Serveur MCP & agent IA** — Serveur Model Context Protocol intégré (`/mcp`) et assistant avec function calling : lisez, cherchez et modifiez vos vaults depuis Claude Desktop, Cursor… avec confirmations two-step, permissions par vault, rate limiting et redaction des secrets ([guide](docs/MCP_GUIDE.md))
|
||||
- **👥 Collaboration temps réel** — Édition simultanée d'un même document (Yjs/CRDT) : curseurs distants colorés, indicateur de présence, fusion sans conflit, reconnexion automatique et persistance serveur ([détail](docs/features/collaboration.md))
|
||||
- **📱 Éditeur mobile natif** — Édition optimisée pour le tactile : barre d'outils Markdown flottante (gras/italique/code/liste/lien), bouton « Coller » persistant (contournement iOS), zoom par pincement et hauteur ajustable, raccourcis swipe (liens entrants / table des matières) et mode lecture plein écran avec navigation entre fichiers ([détail](docs/features/mobile-editor.md))
|
||||
- **🗺️ Vue graphe interactive** — Canvas force-directed avec Barnes-Hut O(n log n), filtres (tag, type), profondeur, mode focus, historique de navigation ←→↑, export PNG, aperçu au survol (Ctrl+click)
|
||||
- **🗂️ Multi-vault** : Visualisez plusieurs vaults Obsidian simultanément
|
||||
- **🌳 Navigation arborescente** : Parcourez vos dossiers et fichiers dans la sidebar
|
||||
- **🔍 Recherche avancée** : Moteur TF-IDF avec stemming français, normalisation des accents, snippets surlignés, facettes, pagination et tri — plus une **recherche sémantique** optionnelle (embeddings `all-MiniLM-L6-v2`, fusion hybride TF-IDF + RRF) activable via le toggle `~` ([détail](docs/features/semantic-search.md))
|
||||
- **💡 Autocomplétion intelligente** : Suggestions de fichiers, tags et historique avec navigation clavier
|
||||
- **🧩 Syntaxe de requête** : Opérateurs `tag:`, `#`, `vault:`, `title:`, `path:`, `ext:` avec chips visuels
|
||||
- **📜 Historique de recherche** : Persisté en localStorage (max 50 entrées, LIFO, dédupliqué)
|
||||
- **🏷️ Tag cloud** : Filtrage par tags extraits des frontmatters YAML
|
||||
- **🔗 Wikilinks** : Les `[[liens internes]]` Obsidian sont cliquables
|
||||
- **🖼️ Images Obsidian** : Support complet des syntaxes d'images Obsidian avec résolution intelligente
|
||||
- **🎨 Diagrammes Excalidraw** : Visualiseur/éditeur natif des fichiers `.excalidraw` et `.excalidraw.md` (iframe sandboxée, auto-save, thème clair/sombre, texte des diagrammes indexé pour la recherche)
|
||||
- **🎨 Syntax highlight** : Coloration syntaxique des blocs de code
|
||||
- **🌓 Thème clair/sombre** : Toggle persisté en localStorage
|
||||
- **📡 Synchronisation temps réel** : Surveillance automatique des fichiers via watchdog avec mise à jour incrémentale de l'index
|
||||
- **📡 Server-Sent Events** : Notifications SSE pour les changements d'index avec reconnexion automatique
|
||||
- **➕ Gestion dynamique des vaults** : Ajout/suppression de vaults via API sans redémarrage
|
||||
- **🖥️ Application desktop native** : Tauri (Rust) + backend Python embarqué, sans Docker ni navigateur
|
||||
- **🐳 Docker multi-platform** : linux/amd64, linux/arm64, linux/arm/v7, linux/386
|
||||
- **🔒 Authentification** : JWT + Argon2id, sessions persistantes, contrôle d'accès par vault
|
||||
- **🛡️ Sécurité** : Rate limiting, audit log, backup automatique, redaction de secrets, headers CSP, protection path traversal, utilisateur non-root
|
||||
- **⚡ Performance** : Compression GZip, Cache-Control immutable, index inversé incrémental, search sans I/O disque
|
||||
- **❤️ Healthcheck** : Endpoint `/api/health` intégré pour Docker et monitoring
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Prérequis
|
||||
|
||||
### Système requis
|
||||
- **Docker** >= 20.10
|
||||
- **docker-compose** >= 2.0
|
||||
- **Espace disque** : ~200MB pour l'image Docker
|
||||
|
||||
### Systèmes supportés
|
||||
- Linux (Ubuntu, Debian, CentOS, etc.)
|
||||
- macOS (Intel et Apple Silicon)
|
||||
- Windows (avec Docker Desktop)
|
||||
- NAS compatibles Docker (Synology, QNAP, etc.)
|
||||
|
||||
---
|
||||
|
||||
## ⚡ Installation rapide
|
||||
|
||||
### 1. Cloner le dépôt
|
||||
|
||||
```bash
|
||||
git clone https://git.dracodev.net/Projets/ObsiGate.git
|
||||
cd ObsiGate
|
||||
```
|
||||
|
||||
### 2. Configurer vos vaults et vos secrets
|
||||
|
||||
Éditez `docker-compose.yml` pour ajouter vos vaults Obsidian :
|
||||
|
||||
```yaml
|
||||
volumes:
|
||||
- /chemin/absolu/vers/votre/vault:/vaults/NomDeVotreVault:ro
|
||||
```
|
||||
|
||||
> **Important** : Le chemin doit être absolu et le volume en lecture seule (`:ro`)
|
||||
|
||||
Créez votre fichier `.env` pour l'authentification et les secrets :
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
# Éditez .env pour configurer vos mots de passe et options
|
||||
```
|
||||
|
||||
> **Ne committez jamais `.env` !** Il est dans `.gitignore`. Utilisez `.env.example` comme référence.
|
||||
|
||||
### 3. Lancer l'application
|
||||
|
||||
```bash
|
||||
chmod +x build.sh # une seule fois
|
||||
./build.sh # build + déploiement en une commande
|
||||
```
|
||||
|
||||
> Options utiles : `./build.sh --help`, `./build.sh --cache` (rebuild rapide), `./build.sh --build-only` (construire sans démarrer).
|
||||
|
||||
### 4. Accéder à l'interface
|
||||
|
||||
Ouvrez votre navigateur sur : **http://localhost:2020**
|
||||
|
||||
---
|
||||
|
||||
## ⚙️ Configuration détaillée
|
||||
|
||||
### Étape 1 : Préparation des vaults
|
||||
|
||||
1. **Localisez vos vaults Obsidian** sur votre système
|
||||
2. **Notez les chemins absolus** vers chaque dossier
|
||||
3. **Vérifiez les permissions** : Docker doit pouvoir lire ces dossiers
|
||||
|
||||
### Étape 2 : Configuration docker-compose.yml
|
||||
|
||||
```yaml
|
||||
services:
|
||||
obsigate:
|
||||
build:
|
||||
context: .
|
||||
image: obsigate:latest
|
||||
container_name: obsigate
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "2020:8080" # Port local 2020 → Port conteneur 8080
|
||||
volumes:
|
||||
- /home/user/Documents/Obsidian-Recettes:/vaults/Recettes:ro
|
||||
- /home/user/Documents/Obsidian-IT:/vaults/IT:ro
|
||||
- ./data:/app/data # Persistance des données d'auth
|
||||
environment:
|
||||
- VAULT_1_NAME=Recettes
|
||||
- VAULT_1_PATH=/vaults/Recettes
|
||||
- VAULT_2_NAME=IT
|
||||
- VAULT_2_PATH=/vaults/IT
|
||||
- OBSIGATE_AUTH_ENABLED=true
|
||||
- OBSIGATE_ADMIN_USER=admin
|
||||
env_file:
|
||||
- .env # Contient OBSIGATE_ADMIN_PASSWORD et autres secrets
|
||||
```
|
||||
|
||||
### Étape 3 : Build & déploiement
|
||||
|
||||
```bash
|
||||
chmod +x build.sh
|
||||
./build.sh
|
||||
```
|
||||
|
||||
**Alternative manuelle :**
|
||||
|
||||
```bash
|
||||
docker compose build --no-cache
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
> **Compatibilité Docker** : l'image utilise une variante minimale d'`uvicorn` et `fastapi 0.110.3` afin d'éviter les dépendances optionnelles natives (`watchfiles`, `uvloop`, …) qui peuvent échouer au build sur Alpine, ARM ou i386.
|
||||
|
||||
---
|
||||
|
||||
## 🌍 Variables d'environnement
|
||||
|
||||
Les vaults sont configurées par paires `VAULT_N_NAME` / `VAULT_N_PATH` (N = 1, 2, 3…) :
|
||||
|
||||
| Variable | Description | Exemple |
|
||||
|----------|-------------|---------|
|
||||
| `VAULT_1_NAME` | Nom affiché de la vault | `Recettes` |
|
||||
| `VAULT_1_PATH` | Chemin dans le conteneur | `/vaults/Obsidian-RECETTES` |
|
||||
| `VAULT_1_ATTACHMENTS_PATH` | Dossier d'attachements (optionnel) | `06_Boite_a_Outils/6.2_Attachments` |
|
||||
| `VAULT_1_SCAN_ATTACHMENTS` | Scan d'images au démarrage (défaut : true) | `true` |
|
||||
|
||||
**Règles de nommage :** lettres, chiffres et tirets uniquement ; pas d'espaces ; le nom doit correspondre au chemin dans le conteneur.
|
||||
|
||||
---
|
||||
|
||||
## 🔒 Authentification
|
||||
|
||||
> **Désactivée par défaut** — Compatible avec toutes les installations existantes.
|
||||
|
||||
Système optionnel basé sur **JWT + Argon2id** avec contrôle d'accès par vault.
|
||||
|
||||
### Activer l'authentification
|
||||
|
||||
1. `cp .env.example .env`
|
||||
2. Éditez `.env` :
|
||||
```bash
|
||||
OBSIGATE_AUTH_ENABLED=true
|
||||
OBSIGATE_ADMIN_USER=admin
|
||||
OBSIGATE_ADMIN_PASSWORD=votre_mot_de_passe # Laissez vide = auto-généré (voir logs)
|
||||
# OBSIGATE_SECURE_COOKIES=false # true si derrière HTTPS
|
||||
```
|
||||
3. Dans `docker-compose.yml` : `env_file: - .env`
|
||||
|
||||
> **Ne mettez jamais de mot de passe dans `docker-compose.yml` !** Utilisez toujours `.env`.
|
||||
|
||||
### Premier démarrage
|
||||
|
||||
Si aucun utilisateur n'existe, ObsiGate crée automatiquement un compte admin et **affiche le mot de passe une seule fois dans les logs** :
|
||||
|
||||
```bash
|
||||
docker-compose logs obsigate | grep -A4 "PREMIER"
|
||||
```
|
||||
|
||||
### Gestion des utilisateurs via CLI
|
||||
|
||||
```bash
|
||||
# Créer un utilisateur
|
||||
docker exec obsigate python backend/create_admin.py create alice MonMotDePasse --role user --vaults Recettes IT
|
||||
|
||||
# Créer un admin avec accès total
|
||||
docker exec obsigate python backend/create_admin.py create bob SecretPass --role admin --vaults "*"
|
||||
|
||||
# Lister / supprimer
|
||||
docker exec obsigate python backend/create_admin.py list
|
||||
docker exec obsigate python backend/create_admin.py delete alice
|
||||
```
|
||||
|
||||
### Interface d'administration
|
||||
|
||||
Un compte **admin** connecté voit une icône 🛡️ dans le header : liste, création/édition/suppression d'utilisateurs, assignation des vaults, activation/désactivation de comptes.
|
||||
|
||||
### Contrôle d'accès par vault
|
||||
|
||||
| Valeur vaults | Accès |
|
||||
|---------------|-------|
|
||||
| `["*"]` | Toutes les vaults (y compris futures) — défaut admin |
|
||||
| `["Recettes", "IT"]` | Uniquement ces vaults |
|
||||
| `[]` | Aucun accès |
|
||||
|
||||
### Variables d'environnement d'auth
|
||||
|
||||
| Variable | Description | Défaut |
|
||||
|----------|-------------|--------|
|
||||
| `OBSIGATE_AUTH_ENABLED` | Activer l'authentification | `false` |
|
||||
| `OBSIGATE_ADMIN_USER` | Nom de l'admin auto-créé | `admin` |
|
||||
| `OBSIGATE_ADMIN_PASSWORD` | Mot de passe admin (vide = auto-généré) | *(auto)* |
|
||||
| `OBSIGATE_SECURE_COOKIES` | Cookie `Secure` (HTTPS uniquement) | `false` |
|
||||
| `OBSIGATE_ACCESS_TOKEN_TTL` | Durée de vie token JWT (secondes) | `3600` |
|
||||
| `OBSIGATE_REFRESH_TOKEN_TTL` | Durée de vie refresh token (secondes) | `2592000` |
|
||||
| `OBSIGATE_LOGIN_MAX_ATTEMPTS` | Tentatives de login max par IP | `10` |
|
||||
| `OBSIGATE_ACCOUNT_MAX_ATTEMPTS` | Tentatives de login max par compte | `10` |
|
||||
| `OBSIGATE_LOGIN_WINDOW_SECONDS` | Fenêtre de rate limiting (secondes) | `900` |
|
||||
| `OBSIGATE_TRUST_PROXY` | Faire confiance à `X-Forwarded-For` pour l'IP client (reverse proxy) | `false` |
|
||||
| `OBSIGATE_WEBHOOK_ALLOW_HTTP` | Autoriser les webhooks non HTTPS | `false` |
|
||||
| `OBSIGATE_WEBHOOK_ALLOW_PRIVATE` | Autoriser les webhooks vers des adresses privées/boucle | `false` |
|
||||
| `OBSIGATE_PDF_MAX_SIZE_MB` | Taille max des PDF extraits (text indexation) | `50` |
|
||||
| `OBSIGATE_PDF_EXTRACT_TIMEOUT` | Timeout extraction PDF (secondes) | `30` |
|
||||
|
||||
### Volume pour la persistance
|
||||
|
||||
Les données d'auth (`users.json`, `secret.key`) sont stockées dans `/app/data` :
|
||||
|
||||
```yaml
|
||||
volumes:
|
||||
- ./data:/app/data # Persistance des utilisateurs et clé JWT
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ➕ Ajouter une nouvelle vault
|
||||
|
||||
### Méthode 1 : Édition directe
|
||||
|
||||
1. `docker-compose down`
|
||||
2. Ajoutez le volume : `- /nouveau/chemin/vault:/vaults/NouvelleVault:ro`
|
||||
3. Ajoutez les variables : `VAULT_4_NAME=NouvelleVault` + `VAULT_4_PATH=/vaults/NouvelleVault`
|
||||
4. Redémarrez : `./build.sh`
|
||||
|
||||
### Méthode 2 : Hot-reload (recommandé)
|
||||
|
||||
1. Ajoutez volume + variables comme ci-dessus
|
||||
2. `./build.sh`
|
||||
3. `curl http://localhost:2020/api/index/reload`
|
||||
|
||||
### Méthode 3 : API dynamique (sans redémarrage)
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:2020/api/vaults/add \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name": "NouvelleVault", "path": "/vaults/NouvelleVault"}'
|
||||
|
||||
curl -X DELETE http://localhost:2020/api/vaults/NouvelleVault
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔨 Build & déploiement avec build.sh
|
||||
|
||||
### Utilisation de base
|
||||
|
||||
```bash
|
||||
chmod +x build.sh # une seule fois
|
||||
./build.sh # build from scratch + démarrage
|
||||
```
|
||||
|
||||
### Options disponibles
|
||||
|
||||
| Option | Description |
|
||||
|--------|-------------|
|
||||
| `--help`, `-h` | Affiche l'aide complète |
|
||||
| `--build-only` | Construit l'image sans démarrer le conteneur |
|
||||
| `--no-cache` | Rebuild complet sans cache Docker **(défaut)** |
|
||||
| `--cache` | Utilise le cache Docker (plus rapide si peu de changements) |
|
||||
| `--progress=plain` | Sortie verbeuse (recommandé pour le debug) |
|
||||
| `--progress=tty` | Sortie interactive avec barres de progression |
|
||||
|
||||
### Ce que fait le script
|
||||
|
||||
1. **Vérifie** Docker et Docker Compose (versions)
|
||||
2. **Valide** `docker-compose.yml` (présence + syntaxe)
|
||||
3. **Vérifie chaque volume** monté (avertit si la source n'existe pas)
|
||||
4. **Construit** l'image Docker (multi-stage, ~180MB)
|
||||
5. **Démarre** le conteneur (`docker compose up -d`)
|
||||
6. **Affiche** statut + logs en temps réel
|
||||
|
||||
### Arrêter / redémarrer
|
||||
|
||||
```bash
|
||||
docker compose down # Arrêter
|
||||
docker compose up -d # Redémarrer sans rebuild
|
||||
docker compose logs -f # Voir les logs
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🖼️ Rendu d'images Obsidian
|
||||
|
||||
ObsiGate supporte **toutes les syntaxes d'images Obsidian** avec résolution intelligente multi-stratégies.
|
||||
|
||||
### Syntaxes supportées
|
||||
|
||||
1. **Markdown standard avec attributs HTML** : `[<img width="180" src="path/to/image.svg"/>](https://example.com)`
|
||||
2. **Wiki-link embed chemin complet** : `![[06_Boite_a_Outils/6.2_Attachments/image.svg]]`
|
||||
3. **Wiki-link embed nom de fichier** : `![[image.svg]]`
|
||||
4. **Markdown standard** : ``
|
||||
|
||||
### Résolution intelligente (7 stratégies, par priorité)
|
||||
|
||||
1. Chemin absolu
|
||||
2. Dossier d'attachements configuré (`VAULT_N_ATTACHMENTS_PATH`)
|
||||
3. Index de démarrage (match unique)
|
||||
4. Même répertoire que le fichier markdown
|
||||
5. Racine du vault
|
||||
6. Index de démarrage (match le plus proche)
|
||||
7. Fallback : placeholder stylisé `[image not found: filename.ext]`
|
||||
|
||||
### Configuration
|
||||
|
||||
```yaml
|
||||
environment:
|
||||
- VAULT_1_NAME=MonVault
|
||||
- VAULT_1_PATH=/vaults/MonVault
|
||||
- VAULT_1_ATTACHMENTS_PATH=Assets/Images # Chemin relatif
|
||||
- VAULT_1_SCAN_ATTACHMENTS=true # Activer le scan (défaut)
|
||||
```
|
||||
|
||||
### Rescan manuel
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:2020/api/attachments/rescan/MonVault
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🖥️ Desktop (Tauri) — Application native
|
||||
|
||||
ObsiGate Desktop est une application native construite avec [Tauri](https://tauri.app/) (Rust + webview système). Elle embarque le backend Python et le frontend dans un exécutable standalone — zéro Docker, zéro ligne de commande.
|
||||
|
||||
> 🚧 **Version 2.0.0 — binaires en cours de stabilisation.** Pour l'instant, le build depuis les sources est recommandé.
|
||||
|
||||
### Fonctionnalités desktop natives
|
||||
|
||||
| Fonctionnalité | Web | Desktop |
|
||||
|---|---|---|
|
||||
| Accès fichiers local | Via upload | Natif (sélecteur dossier) |
|
||||
| Thème système | Manuel | Auto (suit OS dark/light) |
|
||||
| Notifications | Service Worker | Natif OS |
|
||||
| Associations `.md` | ❌ | ✅ « Ouvrir avec ObsiGate » |
|
||||
| Tray icon | ❌ | ✅ Barre des tâches |
|
||||
| Auto-update | ❌ | ✅ Vérifie les releases Gitea |
|
||||
| Mode hors-ligne | Limité | Complet (backend local) |
|
||||
|
||||
### Téléchargement (binaires pré-buildés)
|
||||
|
||||
Les releases sont publiées sur [Gitea](https://git.dracodev.net/Projets/ObsiGate/releases) :
|
||||
|
||||
| Plateforme | Format |
|
||||
|---|---|
|
||||
| **Linux** | `.deb` + `.AppImage` |
|
||||
| **Windows** | `.msi` + `.exe` (NSIS) |
|
||||
|
||||
```bash
|
||||
# Linux — .deb (Debian/Ubuntu/Deepin)
|
||||
sudo dpkg -i obsigate_2.0.0_amd64.deb
|
||||
# Linux — .AppImage (toute distrib)
|
||||
chmod +x ObsiGate_2.0.0_amd64.AppImage && ./ObsiGate_2.0.0_amd64.AppImage
|
||||
```
|
||||
|
||||
```cmd
|
||||
REM Windows : double-cliquer sur ObsiGate_2.0.0_x64.msi (ou le setup NSIS)
|
||||
```
|
||||
|
||||
### Démarrage
|
||||
|
||||
1. **Lancez l'application** depuis le menu ou la ligne de commande
|
||||
2. Le backend Python démarre automatiquement sur `127.0.0.1:17890` (splash « Démarrage… » pendant le boot)
|
||||
3. La fenêtre s'ouvre et charge l'interface ObsiGate
|
||||
4. **Premier lancement** : sélectionnez le dossier de vos vaults Obsidian via le sélecteur natif
|
||||
5. Pour fermer : icône tray → Quitter (arrêt propre du backend)
|
||||
|
||||
### Build depuis les sources
|
||||
|
||||
Guide détaillé : [desktop/README.md](./desktop/README.md).
|
||||
|
||||
#### Prérequis communs
|
||||
|
||||
| Outil | Version | Installation |
|
||||
|---|---|---|
|
||||
| Rust (cargo) | ≥ 1.75 | `rustup` |
|
||||
| Tauri CLI | ≥ 2.0 | `cargo install tauri-cli` |
|
||||
| Git | — | — |
|
||||
| Dépendances système Linux | — | `sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev` |
|
||||
|
||||
> **Important — staging :** `tauri.conf.json` embarque `backend/**` et `frontend/**` **depuis le dossier `desktop/`**.
|
||||
> Les scripts de build copient automatiquement `../backend` et `../frontend` dans `desktop/` avant `cargo tauri build`.
|
||||
> Sans ce staging, le build échoue avec « glob pattern backend/**/* path not found ».
|
||||
|
||||
#### 🪟 Windows — `build-windows.bat`
|
||||
|
||||
```cmd
|
||||
REM Prérequis (via Scoop) : rustup, curl, git
|
||||
scoop install rustup curl git
|
||||
rustup default stable
|
||||
cargo install tauri-cli
|
||||
|
||||
cd desktop
|
||||
build-windows.bat
|
||||
```
|
||||
|
||||
Étapes du script :
|
||||
|
||||
1. Tue les processus Python résiduels (`taskkill /F /IM python.exe`)
|
||||
2. Télécharge **Python 3.11 embed** (python.org) → `desktop\python-embed\` + activation de pip (`python311._pth`)
|
||||
3. `pip install -r ..\backend\requirements.txt` dans l'embed
|
||||
4. **Staging** : copie `..\backend` et `..\frontend` dans `desktop\`
|
||||
5. `cargo tauri build --target x86_64-pc-windows-msvc --bundles nsis`
|
||||
6. Copie `python-embed` à côté de l'exécutable (`target\x86_64-pc-windows-msvc\release\`) pour le mode dev local
|
||||
7. Nettoie les dossiers stagés
|
||||
|
||||
→ **Artefact :** `desktop\target\x86_64-pc-windows-msvc\release\bundle\nsis\ObsiGate_2.0.0_x64-setup.exe`
|
||||
|
||||
#### 🐧 Linux — `build-linux.sh`
|
||||
|
||||
```bash
|
||||
cd desktop
|
||||
chmod +x build-linux.sh
|
||||
./build-linux.sh
|
||||
```
|
||||
|
||||
Étapes du script :
|
||||
|
||||
1. Vérifie Rust + Tauri CLI, installe les dépendances système (apt)
|
||||
2. Crée un venv Python `desktop/python-embed/venv` + `pip install -r ../backend/requirements.txt`
|
||||
3. **Staging** : copie `../backend` et `../frontend` dans `desktop/`
|
||||
4. `cargo tauri build --target x86_64-unknown-linux-gnu --bundles deb,appimage`
|
||||
5. Copie le runtime (`python-embed/`, `backend/`, `frontend/`) à côté de l'exécutable
|
||||
|
||||
→ **Artefacts :**
|
||||
|
||||
- `desktop/target/x86_64-unknown-linux-gnu/release/bundle/deb/obsigate_2.0.0_amd64.deb`
|
||||
- `desktop/target/x86_64-unknown-linux-gnu/release/bundle/appimage/ObsiGate_2.0.0_amd64.AppImage`
|
||||
|
||||
### 🤖 Builds CI/CD — artefacts automatiques
|
||||
|
||||
**Oui** — le workflow [`.gitea/workflows/desktop-build.yml`](./.gitea/workflows/desktop-build.yml) construit les binaires desktop à chaque push sur `main` touchant `desktop/**`, `frontend/**` ou `backend/**` (et manuellement via `workflow_dispatch`), sur des **runners self-hosted** :
|
||||
|
||||
| Job | Runner | Artefacts (conservés 30 jours) |
|
||||
|---|---|---|
|
||||
| `build-windows` | `[self-hosted, windows, desktop]` | `desktop/target/release/bundle/msi/*.msi` |
|
||||
| `build-linux` | `[self-hosted, linux, desktop]` | `*.AppImage` + `*.deb` |
|
||||
|
||||
- Les artefacts sont téléchargeables depuis la page **Actions** du run Gitea.
|
||||
- La publication en **Gitea Release** est prévue sur les tags `v*` (étape `Publish to Gitea Release`).
|
||||
- Le workflow web [`.gitea/workflows/ci.yml`](./.gitea/workflows/ci.yml) gère de son côté lint → tests → sécurité → build Docker → e2e Playwright.
|
||||
|
||||
### Architecture Desktop
|
||||
|
||||
```
|
||||
┌────────────────────────────────────────────┐
|
||||
│ Tauri (Rust) │
|
||||
│ ├─ Webview (webview système) │
|
||||
│ │ └─ Frontend (HTML/JS/CSS) │
|
||||
│ └─ Sidecar Python │
|
||||
│ └─ uvicorn backend.main:app │
|
||||
│ └─ port 127.0.0.1:17890 │
|
||||
└────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
Cycle de vie : Tauri spawn le backend Python → health check → splash de démarrage → redirection vers la webview. À la fermeture : kill propre du backend.
|
||||
|
||||
---
|
||||
|
||||
## 📖 Utilisation
|
||||
|
||||
### Interface web
|
||||
|
||||
1. **Navigation** : Cliquez sur les vaults dans la sidebar pour les développer
|
||||
2. **Recherche** : Utilisez la barre de recherche pour chercher dans toutes les vaults
|
||||
3. **Tags** : Cliquez sur les tags pour filtrer les contenus
|
||||
4. **Wikilinks** : Les liens `[[page]]` sont cliquables et navigables
|
||||
5. **Images** : Toutes les syntaxes d'images Obsidian sont rendues automatiquement
|
||||
6. **Thème** : Basculez entre thème clair/sombre avec l'icône 🌙/☀️
|
||||
|
||||
### Raccourcis clavier
|
||||
|
||||
| Action | Raccourci |
|
||||
|--------|-----------|
|
||||
| Recherche | `Ctrl + K` ou `/` |
|
||||
| Toggle thème | `Ctrl + T` |
|
||||
| Focus recherche | `Esc` |
|
||||
|
||||
---
|
||||
|
||||
## 👥 Collaboration temps réel
|
||||
|
||||
Plusieurs utilisateurs peuvent éditer le même document markdown simultanément (façon Google Docs) :
|
||||
|
||||
- **Fusion sans conflit** grâce à Yjs (CRDT) : deux personnes peuvent taper au même endroit, aucune
|
||||
modification n'est perdue.
|
||||
- **Curseurs distants colorés** et sélections visibles dans CodeMirror, avec le nom de chaque
|
||||
utilisateur.
|
||||
- **Indicateur de présence** dans l'en-tête de l'éditeur (avatars + statut de connexion).
|
||||
- **Reconnexion automatique** (backoff exponentiel) : l'état est fusionné au retour.
|
||||
- **Persistance serveur** : le document est écrit sur disque 2 s après la dernière modification.
|
||||
- **Transport** : WebSocket `ws(s)://<hôte>/ws/collab/{vault}/{chemin}`, authentifié par cookie
|
||||
`access_token` (ou `?token=`) et soumis au contrôle d'accès par vault.
|
||||
|
||||
Aucune configuration n'est nécessaire : ouvrez le même fichier dans deux navigateurs (ou deux
|
||||
fenêtres) pour voir la collaboration en action.
|
||||
|
||||
---
|
||||
|
||||
## 🔌 API
|
||||
|
||||
ObsiGate expose une API REST complète :
|
||||
|
||||
| Endpoint | Description | Méthode | Auth |
|
||||
|----------|-------------|---------|------|
|
||||
| `/api/health` | Health check (status, version, stats) | GET | Non |
|
||||
| `/api/auth/status` | Statut auth (activé, utilisateurs présents) | GET | Non |
|
||||
| `/api/auth/login` | Connexion (access token + cookie refresh) | POST | Non |
|
||||
| `/api/auth/refresh` | Renouveler l'access token via cookie refresh | POST | Cookie |
|
||||
| `/api/auth/logout` | Déconnexion + révocation refresh token | POST | Oui |
|
||||
| `/api/auth/me` | Infos utilisateur courant | GET | Oui |
|
||||
| `/api/auth/change-password` | Changer son mot de passe | POST | Oui |
|
||||
| `/api/auth/admin/users` | Lister / créer des utilisateurs | GET/POST | Admin |
|
||||
| `/api/auth/admin/users/{u}` | Modifier / supprimer un utilisateur | PATCH/DELETE | Admin |
|
||||
| `/api/vaults` | Liste des vaults (filtrée par permissions) | GET | Oui |
|
||||
| `/api/browse/{vault}?path=` | Navigation dans les dossiers | GET | Oui |
|
||||
| `/api/file/{vault}?path=` | Contenu rendu d'un fichier | GET | Oui |
|
||||
| `/api/file/{vault}/raw?path=` | Contenu brut d'un fichier | GET | Oui |
|
||||
| `/api/file/{vault}/download?path=` | Téléchargement d'un fichier | GET | Oui |
|
||||
| `/api/file/{vault}/save?path=` | Sauvegarder un fichier | PUT | Oui |
|
||||
| `/api/file/{vault}?path=` | Supprimer un fichier | DELETE | Oui |
|
||||
| `/api/search/advanced` | Recherche avancée TF-IDF (+ `semantic=true` pour l'hybride) | GET | Oui |
|
||||
| `/api/suggest` / `/api/tags/suggest` | Autocomplétion | GET | Oui |
|
||||
| `/api/tags?vault=` | Tags uniques avec compteurs | GET | Oui |
|
||||
| `/api/index/reload` | Force un re-scan des vaults | GET | Admin |
|
||||
| `/api/events` | Flux SSE temps réel | GET | Oui |
|
||||
| `/api/vaults/add` / `/api/vaults/{name}` | Gestion dynamique des vaults | POST/DELETE | Admin |
|
||||
| `/api/image/{vault}?path=` | Servir une image | GET | Oui |
|
||||
| `/api/config` | Lire / écrire la configuration | GET/POST | Oui/Admin |
|
||||
| `/api/diagnostics` | Statistiques index et mémoire | GET | Admin |
|
||||
|
||||
> Quand `OBSIGATE_AUTH_ENABLED=false`, tous les endpoints sont accessibles sans token.
|
||||
> Tous les endpoints exposent des schémas Pydantic documentés ; doc interactive sur `/docs` (Swagger UI).
|
||||
|
||||
**Exemples :**
|
||||
|
||||
```bash
|
||||
curl http://localhost:2020/api/health
|
||||
curl http://localhost:2020/api/vaults
|
||||
curl "http://localhost:2020/api/search/advanced?q=recette%20tag:cuisine&vault=all&limit=20&offset=0&sort=relevance"
|
||||
curl "http://localhost:2020/api/suggest?q=piz&vault=all"
|
||||
curl "http://localhost:2020/api/file/Recettes?path=pizza.md"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔍 Recherche avancée
|
||||
|
||||
### Syntaxe de requête
|
||||
|
||||
| Opérateur | Description | Exemple |
|
||||
|-----------|-------------|---------|
|
||||
| `tag:<nom>` | Filtrer par tag | `tag:recette docker` |
|
||||
| `#<nom>` | Raccourci tag | `#linux serveur` |
|
||||
| `vault:<nom>` | Filtrer par vault | `vault:IT kubernetes` |
|
||||
| `title:<texte>` | Filtrer par titre | `title:pizza` |
|
||||
| `path:<texte>` | Filtrer par chemin | `path:recettes/soupes` |
|
||||
| `ext:<type>` | Filtrer par type de fichier | `ext:md kubernetes` |
|
||||
| `"phrase exacte"` | Recherche de phrase | `tag:"multi mots"` |
|
||||
|
||||
Les opérateurs sont combinables : `tag:linux vault:IT ext:md serveur web`.
|
||||
|
||||
### Support PDF
|
||||
|
||||
Les fichiers PDF de vos vaults s'affichent en ligne dans le navigateur via le visualiseur PDF natif (iframe + `<embed>`).
|
||||
Le visualiseur streame le fichier via HTTP Range (206 Partial Content) — les gros PDF se chargent progressivement.
|
||||
Le texte est extrait à l'indexation (pypdf / pymupdf) — le contenu PDF est donc recherchable via la recherche full-text.
|
||||
Filtrez avec `ext:pdf` pour restreindre les résultats aux PDF.
|
||||
Les métadonnées (pages, titre, auteur) sont disponibles via `GET /api/file/{vault}/pdf/info` sans transférer le document.
|
||||
|
||||
**Limitations :** pas d'OCR (les PDF scannés ne sont pas recherchables), pas d'annotation, pas d'édition du PDF lui-même.
|
||||
|
||||
### Diagrammes Excalidraw
|
||||
|
||||
Les fichiers `.excalidraw` et `.excalidraw.md` s'ouvrent dans un éditeur visuel Excalidraw complet, intégré dans une iframe sandboxée — dessinez, modifiez et sauvegardez sans quitter ObsiGate.
|
||||
Les modifications sont sauvegardées automatiquement (2 s) ou avec `Ctrl+S` ; l'éditeur suit le thème clair/sombre.
|
||||
Le texte des éléments du diagramme est extrait à l'indexation : il est donc recherchable via la recherche full-text (`ext:excalidraw`).
|
||||
Les fichiers créés avec le **plugin Obsidian Excalidraw** (y compris le format `.excalidraw.md` compressé) sont compatibles.
|
||||
|
||||
### Raccourcis clavier
|
||||
|
||||
| Raccourci | Action |
|
||||
|-----------|--------|
|
||||
| `Ctrl+K` / `Cmd+K` | Focaliser la barre de recherche |
|
||||
| `/` | Focaliser la recherche (hors champ texte) |
|
||||
| `↑` / `↓` | Naviguer dans les suggestions |
|
||||
| `Enter` | Sélectionner la suggestion active ou lancer la recherche |
|
||||
| `Escape` | Fermer les suggestions / quitter la recherche |
|
||||
|
||||
### Fonctionnalités
|
||||
|
||||
- **TF-IDF** : scoring par fréquence pondérée des termes
|
||||
- **Boost titre** : correspondances dans le titre ×3
|
||||
- **Normalisation des accents** : `resume` trouve `résumé`
|
||||
- **Snippets surlignés** (`<mark>`), **facettes** (compteurs par vault/tag), **pagination** (50/page), **tri** pertinence/date, **chips** de filtres, **historique** (50 recherches)
|
||||
- **Recherche sémantique** (optionnelle) : le toggle `~` (ou `Alt+S`) fusionne le classement
|
||||
TF-IDF avec un classement par embeddings (RRF). Fonctionne sans dépendance avec un provider de
|
||||
hachage ; installez `backend/requirements-semantic.txt` et/ou renseignez `OBSIGATE_EMBEDDING_*`
|
||||
pour de vrais embeddings `all-MiniLM-L6-v2`. Voir
|
||||
[docs/features/semantic-search.md](docs/features/semantic-search.md).
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Dépannage
|
||||
|
||||
**Port déjà utilisé :**
|
||||
|
||||
```bash
|
||||
sudo netstat -tulpn | grep 2020
|
||||
# Changer le port dans docker-compose.yml : ports: - "2021:8080"
|
||||
```
|
||||
|
||||
**Vault non trouvée :** chemins absolus, permissions de lecture, redémarrer le conteneur après modification.
|
||||
|
||||
**Build échoue :**
|
||||
|
||||
```bash
|
||||
docker system prune -f
|
||||
docker compose down
|
||||
./build.sh --progress=plain
|
||||
# Si l'échec persiste :
|
||||
./build.sh --progress=plain 2>&1 | tee build.log
|
||||
```
|
||||
|
||||
**Logs pour debugging :**
|
||||
|
||||
```bash
|
||||
docker compose logs -f obsigate
|
||||
docker compose logs --tail=100 obsigate
|
||||
```
|
||||
|
||||
**Desktop :** les logs du backend sont dans `%APPDATA%\ObsiGate\logs\backend.log` (Windows) / `~/.config/obsigate/logs/backend.log` (Linux).
|
||||
|
||||
---
|
||||
|
||||
## ⚡ Performance
|
||||
|
||||
| Métrique | Estimation |
|
||||
|----------|------------|
|
||||
| **Indexation** | ~1–2s pour 1 000 fichiers markdown |
|
||||
| **Recherche avancée** | < 10ms pour la plupart des requêtes (index inversé + TF-IDF) |
|
||||
| **Résolution wikilinks** | O(1) via table de lookup |
|
||||
| **Mémoire** | ~80–150MB par 1 000 fichiers (contenu capé à 100 KB/fichier) |
|
||||
| **Image Docker** | ~180MB (multi-stage) |
|
||||
| **CPU** | Non-bloquant ; recherche offloadée sur thread pool dédié |
|
||||
|
||||
### Paramètres recommandés par taille de vault
|
||||
|
||||
| Taille | Fichiers | `search_workers` | `prefix_max_expansions` | `max_content_size` |
|
||||
|--------|----------|-------------------|--------------------------|---------------------|
|
||||
| Petit | < 500 | 1 | 50 | 100 000 |
|
||||
| Moyen | 500–5 000 | 2 | 50 | 100 000 |
|
||||
| Grand | 5 000+ | 4 | 30 | 50 000 |
|
||||
|
||||
Configurables via l'interface (Settings) ou l'API `/api/config`.
|
||||
|
||||
### Optimisations clés
|
||||
|
||||
- **Index inversé avec set-intersection** + prefix matching par recherche binaire
|
||||
- **ThreadPoolExecutor** : recherche CPU-bound hors de l'event loop asyncio
|
||||
- **InvertedIndex incrémental** : hooks `add_document`/`remove_document`, plus de rebuild O(N)
|
||||
- **Compression GZip** (~70% de bande passante économisée) + **Cache-Control immutable** (1 an)
|
||||
- **Race condition guard** (`currentSearchId` + AbortController), progress bar, timeout 30s
|
||||
- **Rendu Markdown singleton**, debounced icon rendering, recherche sans I/O disque
|
||||
|
||||
---
|
||||
|
||||
## 🛡️ Sécurité
|
||||
|
||||
- **Path traversal** : tous les endpoints fichier valident que le chemin résolu reste dans la vault
|
||||
- **Rate limiting** : 10 tentatives de login max par IP sur 15 minutes + lockout par compte (5 tentatives)
|
||||
- **Audit log** : écritures/suppressions/config journalisées dans `data/audit.log` (JSON lines, rotation 10 MB)
|
||||
- **Backup automatique** : chaque modification/suppression sauvegardée dans `.obsigate-backup/` avec timestamp
|
||||
- **Secret redaction** : masquage automatique des JWT, clés API, tokens dans les aperçus
|
||||
- **Utilisateur non-root** : conteneur Docker sous `obsigate` (UID 1000)
|
||||
- **Volumes read-only** : vaults montées en `:ro` par défaut
|
||||
- **Secrets dans `.env`** : jamais dans `docker-compose.yml`
|
||||
- **Atomic writes** : tmp+replace pour users.json, shares.json, webhooks.json
|
||||
|
||||
---
|
||||
|
||||
## 🏗️ Stack technique
|
||||
|
||||
- **Backend** : Python 3.11 + FastAPI 0.110 + Uvicorn
|
||||
- **Auth** : python-jose (JWT HS256) + argon2-cffi (Argon2id)
|
||||
- **File Watcher** : watchdog 4.x (inotify natif + fallback polling)
|
||||
- **Frontend** : Vanilla JS + HTML + CSS (zéro framework, zéro build)
|
||||
- **Rendu Markdown** : mistune 3.x
|
||||
- **PDF Export** : WeasyPrint 60+
|
||||
- **Desktop** : Tauri v2 (Rust) + Python embarqué
|
||||
- **Image Docker** : python:3.11-slim (multi-stage)
|
||||
- **Stockage utilisateurs** : JSON local (`data/users.json`) — aucune base de données
|
||||
- **Architecture** : SPA + API REST + SSE
|
||||
|
||||
---
|
||||
|
||||
## 🏠 Architecture
|
||||
|
||||
```
|
||||
┌─────────────────┐ ┌─────────────────────────────────────────┐
|
||||
│ Navigateur │◄───►│ FastAPI (backend/main.py) │
|
||||
│ (SPA) │ REST │ ┌──────────────┐ ┌──────────────┐ │
|
||||
│ │ │ │ indexer.py │ │ search.py │ │
|
||||
│ app.js │ │ │ (scan+cache) │ │ (in-memory) │ │
|
||||
│ style.css │ │ └───────┬────── └──────┬───────┘ │
|
||||
│ index.html │ │ │ │ │
|
||||
└─────────────────┘ │ ┌───┴──────────────┴───┐ │
|
||||
│ │ Index en mémoire │ │
|
||||
│ │ (fichiers, tags, │ │
|
||||
│ │ contenu, lookup) │ │
|
||||
│ └───────────────────────┘ │
|
||||
└─────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
**Flux de données :**
|
||||
|
||||
1. Au démarrage, `indexer.py` scanne tous les vaults en parallèle (thread pool, non-bloquant)
|
||||
2. Contenu, tags (YAML + inline) et métadonnées mis en cache en mémoire
|
||||
3. Table de lookup O(1) pour la résolution des wikilinks
|
||||
4. `watcher.py` surveille les fichiers (watchdog natif ou polling)
|
||||
5. Modifications → mise à jour incrémentale de l'index
|
||||
6. Changements notifiés au frontend via SSE
|
||||
7. Recherche sur l'index mémoire (zéro I/O disque)
|
||||
8. Le frontend SPA communique via REST + SSE
|
||||
|
||||
---
|
||||
|
||||
## 📝 Développement
|
||||
|
||||
### 🧪 Tests et CI — règles obligatoires (avant chaque push)
|
||||
|
||||
> ⚠️ **Règle absolue — contributeurs humains et agents IA** : ne jamais faire
|
||||
> de `git push` sans avoir exécuté **localement** la séquence complète
|
||||
> ci-dessous avec **100 % de succès**. Le push ne sert pas à découvrir les
|
||||
> échecs : la CI est un filet de sécurité, pas un outil de diagnostic.
|
||||
|
||||
#### Vérifications locales avant commit
|
||||
|
||||
| Étape | Commande | Job CI équivalent |
|
||||
|---|---|---|
|
||||
| Lint backend | `ruff check backend/` | `lint` |
|
||||
| Validation des imports frontend | `node tests/frontend/validate-imports.mjs` | `lint` |
|
||||
| Tests unitaires frontend | `node tests/frontend/unit.test.mjs` | `lint` |
|
||||
| Tests backend | `pytest tests/ -q` | `test` |
|
||||
| **E2E Playwright** | `npm run test:e2e` (~5 min) | `e2e` |
|
||||
|
||||
#### Tests E2E locaux (`npm run test:e2e`)
|
||||
|
||||
Le script `scripts/run-e2e-local.sh` reproduit fidèlement les conditions du
|
||||
job CI `e2e` (`.gitea/workflows/ci.yml`) :
|
||||
|
||||
- backend démarré nativement (venv `.venv-e2e`, Python 3.11 via `uv`) ;
|
||||
- authentification désactivée (`OBSIGATE_AUTH_ENABLED=false`) ;
|
||||
- fixtures `test_vault` (TestVault) et `test_dir` (TestDir) ;
|
||||
- port `2029` (baseURL de `playwright.config.ts`) ;
|
||||
- projet Playwright `chromium-desktop` uniquement (comme la CI).
|
||||
|
||||
Prérequis : `uv`, Node.js ≥ 20, navigateurs Playwright
|
||||
(`npx playwright install chromium`). Le venv est créé automatiquement au
|
||||
premier lancement. Docker n'est pas nécessaire.
|
||||
|
||||
```bash
|
||||
npm run test:e2e # suite complète
|
||||
bash scripts/run-e2e-local.sh --headed # navigateur visible
|
||||
bash scripts/run-e2e-local.sh -g "reset panes" # filtre sur un test
|
||||
```
|
||||
|
||||
La suite doit se terminer sur **tous les tests passant** (60 actuellement),
|
||||
sans échec ni dépendance aux retries. En cas d'échec : corriger et relancer
|
||||
localement jusqu'à 100 %, puis seulement commiter.
|
||||
|
||||
#### Règles d'écriture des tests (leçon apprise)
|
||||
|
||||
- **Vérifier chaque sélecteur dans le DOM réel** avant de l'utiliser dans un
|
||||
test. Bug réel rencontré : un test ciblait `#cp-input` alors que l'input de
|
||||
la palette porte uniquement la classe `.cp-input` (aucun id) — le test ne
|
||||
pouvait passer dans aucun environnement.
|
||||
- Tout test nouveau ou modifié doit être **exécuté localement** (au minimum
|
||||
avec `-g "nom du test"`) avant le push.
|
||||
- Pas de contournements qui masquent une instabilité (`waitForTimeout`
|
||||
arbitraires, fallback silencieux) : traiter la cause racine.
|
||||
|
||||
#### Commits et push
|
||||
|
||||
- Message de commit : sujet à l'impératif ≤ 50 caractères ; corps (72 col.)
|
||||
uniquement s'il apporte une information utile.
|
||||
- Un commit = un changement logique (ne pas mêler fix et refactoring).
|
||||
- Push uniquement quand les 5 étapes locales sont vertes. Pipeline CI :
|
||||
`lint → test → security → build → e2e`.
|
||||
|
||||
### Structure du projet
|
||||
|
||||
```
|
||||
ObsiGate/
|
||||
├── backend/ # API FastAPI
|
||||
│ ├── main.py # Endpoints, Pydantic models, rendu markdown
|
||||
│ ├── indexer.py # Scan des vaults, index en mémoire, lookup table
|
||||
│ ├── search.py # Moteur de recherche fulltext avec scoring
|
||||
│ ├── watcher.py # Surveillance fichiers (watchdog + debounce)
|
||||
│ ├── auth/ # Module d'authentification (JWT + Argon2id)
|
||||
│ └── create_admin.py # CLI gestion utilisateurs
|
||||
├── frontend/ # Interface web (Vanilla JS, zéro framework)
|
||||
│ ├── index.html # Page SPA + écran de login + splash de boot
|
||||
│ ├── js/ # Modules ES (app, viewer, editor, graph, …)
|
||||
│ └── style.css # Styles (CSS variables, thèmes, responsive)
|
||||
├── desktop/ # Application native Tauri (Rust + Python embarqué)
|
||||
│ ├── src/main.rs # Shell natif : cycle de vie backend, tray, menus
|
||||
│ ├── build-windows.bat
|
||||
│ └── build-linux.sh
|
||||
├── data/ # Données persistantes (créé au démarrage)
|
||||
├── Dockerfile # Multi-stage, healthcheck, non-root
|
||||
├── docker-compose.yml # Déploiement avec healthcheck et auth env vars
|
||||
├── build.sh # Build & déploiement automatisé
|
||||
└── docs/ # ROADMAP, guides, audits
|
||||
```
|
||||
|
||||
### Contribuer
|
||||
|
||||
Voir [docs/CONTRIBUTING.md](./docs/CONTRIBUTING.md) pour les standards de code et
|
||||
[docs/DELIVERY_WORKFLOW.md](./docs/DELIVERY_WORKFLOW.md) pour la méthode de livraison obligatoire.
|
||||
|
||||
---
|
||||
|
||||
## 📄 Licence
|
||||
|
||||
Ce projet est sous licence **MIT** — voir le fichier [LICENSE](LICENSE) pour les détails.
|
||||
|
||||
---
|
||||
|
||||
## 🤝 Support
|
||||
|
||||
- **Issues** : [git.dracodev.net/Projets/ObsiGate/issues](https://git.dracodev.net/Projets/ObsiGate/issues)
|
||||
- **Documentation** : [git.dracodev.net/Projets/ObsiGate/wiki](https://git.dracodev.net/Projets/ObsiGate/wiki)
|
||||
|
||||
---
|
||||
|
||||
## 📝 Changelog
|
||||
|
||||
Consultez le [CHANGELOG.md](./CHANGELOG.md) pour l'historique complet de toutes les versions (v1.0.0 → v2.4.0).
|
||||
|
||||
---
|
||||
|
||||
*Projet : ObsiGate | Version : 2.4.0 | Dernière mise à jour : Juin 2026*
|
||||
-198
@@ -1,198 +0,0 @@
|
||||
# ObsiGate — Roadmap
|
||||
|
||||
> **Version :** 1.8.0 | **Dernière mise à jour :** 2026-06-03
|
||||
> Voir aussi [CHANGELOG.md](./CHANGELOG.md), [docs/ROADMAP.md](./docs/ROADMAP.md), [AUDIT_TECHNIQUE.md](./docs/AUDIT_TECHNIQUE_2026-05-27.md)
|
||||
|
||||
---
|
||||
|
||||
## ✅ Complété (v1.0.0 → v1.7.0)
|
||||
|
||||
### AI Editor (v1.7.0)
|
||||
- [x] Toolbar dropdown menus : Edit, Tone, Translate, Generate, Rewrite, Toolbox
|
||||
- [x] Multi-provider : DeepSeek, OpenRouter, Gemini
|
||||
- [x] 16 endpoints REST `/api/ai/{action}` + backend `ai.py` / `ai_routes.py`
|
||||
- [x] Auto-save silencieux (2s debounce), loading toasts, cache toolbar si pas de clé API
|
||||
- [x] Messages d'erreur clairs (401 → clé invalide, etc.)
|
||||
|
||||
### Vue graphe — Phases 3 & 4 (v1.6.0)
|
||||
- [x] Filtre par type (dossier/fichier/.md/autre), mode focus, plein écran, export PNG
|
||||
- [x] Barnes-Hut O(n log n), cache graphe
|
||||
- [x] Header flat design, sticky panels, navigation historique ← → ↑
|
||||
- [x] Ctrl+survol → aperçu contenu formaté
|
||||
|
||||
### Performance & CI/CD (v1.6.0)
|
||||
- [x] `sortedcontainers` — O(log n) insert/remove dans l'index inversé
|
||||
- [x] CI/CD Pipeline Gitea Actions (lint → test → security → build)
|
||||
- [x] Ruff linting — 0 erreur
|
||||
- [x] Mypy type checking — 0 erreur
|
||||
- [x] Pytest — **284 tests, 70% coverage** (main.py 61%, router.py 67%, watcher.py 62%)
|
||||
- [x] Tests API main.py (84 tests), Auth API (13 tests), Watcher mocké (14 tests)
|
||||
- [x] Bandit SAST + Pip-audit + Docker build + coverage artifact
|
||||
|
||||
### Split app.js en modules ES (v1.5.1)
|
||||
- [x] 8 875 lignes → 16 modules dans `frontend/js/`
|
||||
- [x] Validateur imports/exports CI
|
||||
- [x] Tests unitaires frontend (Node.js)
|
||||
|
||||
### Stemming français (v1.5.1)
|
||||
- [x] `snowballstemmer` — recherche accent-insensitive et morphologique
|
||||
- [x] Performance O(T) au lieu de O(S×T), crash guard IndexError
|
||||
|
||||
### Fonctionnalités avancées (v1.5.0)
|
||||
- [x] Publication publique de documents (lien partageable avec token)
|
||||
- [x] Webhooks HTTP avec signature HMAC-SHA256
|
||||
- [x] Dashboard statistiques (fichiers, tags, taille, vaults)
|
||||
- [x] Documentation OpenAPI enrichie (Swagger + Redoc)
|
||||
- [x] Gestion des conflits Syncthing
|
||||
- [x] Index inversé incrémental (hook pattern)
|
||||
|
||||
### Sécurité (v1.4.0)
|
||||
- [x] JWT + Argon2id
|
||||
- [x] Rate limiting login (IP + compte)
|
||||
- [x] Audit log avec rotation
|
||||
- [x] Backup automatique avant écriture
|
||||
- [x] Redaction de secrets
|
||||
- [x] Headers CSP
|
||||
- [x] Protection path traversal
|
||||
- [x] Utilisateur non-root Docker
|
||||
|
||||
### UX / Qualité
|
||||
- [x] `.dockerignore`, `.env` → `.env.example`, Ruff config, Mypy config
|
||||
- [x] Compression GZip (SSE-safe), Cache-Control immutable
|
||||
- [x] Recherche sans I/O disque
|
||||
- [x] Backlinks panel
|
||||
- [x] Fichiers non-supportés (UI download)
|
||||
|
||||
---
|
||||
|
||||
## 🔜 Prochaines étapes (par priorité)
|
||||
|
||||
### ✅ 1. Tests & Couverture — FAIT
|
||||
- Coverage **49% → 70%** ✅
|
||||
- ~~Cibles : main.py (26%), watcher.py (23%), router.py (37%)~~ — **FAIT**
|
||||
- ~~Tests E2E Playwright (fondations)~~ — reste à faire
|
||||
|
||||
### ✅ 2. Export PDF — FAIT
|
||||
- WeasyPrint déjà intégré ✅
|
||||
- Route API `/api/file/{vault_name}/pdf` ✅
|
||||
- Bouton UI "PDF" dans les actions fichier ✅
|
||||
- Export dans la vue publique `/s/{token}/pdf` ✅
|
||||
- GTK/Pango installé dans le Dockerfile ✅
|
||||
|
||||
### ✅ 3. Palette de commandes (Ctrl+ALT+SPACE) — FAIT
|
||||
- Navigation rapide fichiers + commandes (> prefix) ✅
|
||||
- Module `frontend/js/palette.js` + intégré dans app.js ✅
|
||||
|
||||
### ✅ 4. Barre d'outils mobile + Palette fichiers/commandes 📱 — FAIT
|
||||
- **Effort** : 3-5 jours | **Impact** : 🟡 UX mobile
|
||||
- **Fichiers** : `frontend/js/mobile-toolbar.js`, `frontend/index.html`, `frontend/style.css`, `frontend/js/app.js`
|
||||
|
||||
#### ✅ Phase 1 — Barre de navigation persistante
|
||||
- [x] **Bottom bar fixe** : barre collée en bas de l'écran sur mobile/tablette (responsive, breakpoint < 768px)
|
||||
- [x] **Icônes** : explorateur 📂, recherche 🔍, palette ⌨️, onglets 📑
|
||||
- [x] **Compteur d'onglets** : badge indiquant le nombre de fichiers ouverts
|
||||
- [x] **safe-area-inset-bottom** pour les iPhone avec notch
|
||||
- [x] **backdrop-filter blur** pour l'effet vitré
|
||||
|
||||
#### ✅ Phase 2 — Page d'accueil vault (quand aucun fichier ouvert)
|
||||
- [x] **Dashboard existant** réutilisé : showWelcome() affiche stats + récents + bookmarks
|
||||
- [x] **Palette accessible** depuis la barre pour ouvrir/créer des fichiers
|
||||
|
||||
#### ✅ Phase 3 — Palette de fichiers (Ctrl+P style)
|
||||
- [x] **Recherche rapide** via `/api/suggest` avec opérateurs +include -exclude
|
||||
- [x] **Raccourci** : bouton ⌨️ dans la barre du bas + tap pour fichiers, long-press pour commandes
|
||||
- [x] **Résultats** avec preview du chemin (vault/path)
|
||||
- [x] **Navigation clavier** : flèches ↑↓ + Entrée pour ouvrir, browse répertoires
|
||||
|
||||
#### ✅ Phase 4 — Palette de commandes (Ctrl+Shift+P style)
|
||||
- [x] **Liste des actions** : 12 commandes (créer/supprimer fichier/dossier, thème, réindex, etc.)
|
||||
- [x] **Long-press** sur le bouton palette → palette de commandes
|
||||
- [x] **Clic droit** sur le bouton palette → palette de commandes
|
||||
- [x] **Fuzzy search** : filtrer les commandes en tapant
|
||||
|
||||
#### ✅ Phase 5 — Gestion des fichiers ouverts (tabs)
|
||||
- [x] **Indicateur d'onglets** dans la barre du bas : badge compteur
|
||||
- [x] **Switcher rapide** : bottom sheet animé avec liste des fichiers ouverts
|
||||
- [x] **Fermeture** : bouton ✕ par onglet dans le switcher
|
||||
- [x] **Overlay** : tap à l'extérieur pour fermer le sheet
|
||||
|
||||
### ✅ 5. Drag & drop de fichiers — FAIT
|
||||
- **Effort** : 1-2 jours | **Impact** : 🟡
|
||||
- Déplacer/réorganiser dans l'arborescence ✅
|
||||
- **Fichiers** : `backend/main.py` (endpoint `POST /api/move/{vault_name}`), `frontend/js/dragdrop.js`, `frontend/js/app.js`, `frontend/style.css`
|
||||
- [x] **Endpoint backend** : `POST /api/move/{vault_name}` — déplace fichiers et dossiers vers un autre répertoire parent, même vault
|
||||
- [x] **Drag & drop frontend** : glisser-déposer les `.tree-item` (fichiers et dossiers) vers les dossiers ou la racine d'un vault
|
||||
- [x] **Indicateurs visuels** : opacité réduite sur l'élément source, outline en pointillé sur les cibles valides, surbrillance sur la cible active
|
||||
- [x] **Protection** : pas de drop sur soi-même, pas de drop dans un enfant, pas de cross-vault
|
||||
- [x] **SSE + index** : mise à jour de l'index et broadcast SSE après move
|
||||
|
||||
### ✅ 6. Diff viewer backups — FAIT
|
||||
- **Effort** : 1 jour | **Impact** : 🟢
|
||||
- Comparer versions sauvegardées dans `.obsigate-backup/` ✅
|
||||
- **Fichiers** : `backend/main.py` (3 endpoints + helpers), `frontend/js/backups.js`, `frontend/js/ui.js`, `frontend/style.css`
|
||||
- [x] **`GET /api/file/{vault}/backups`** — liste tous les backups horodatés d'un fichier (triés plus récent d'abord)
|
||||
- [x] **`GET /api/file/{vault}/diff`** — diff unifié entre deux versions (backup vs backup, ou backup vs actuel)
|
||||
- [x] **`POST /api/file/{vault}/restore`** — restaure une version backup (backup automatique de la version actuelle avant restauration)
|
||||
- [x] **UI modale** : panneau avec sélecteur gauche (backup) / droite (version actuelle ou autre backup), diff coloré (vert/rouge), bouton Restaurer avec confirmation
|
||||
- [x] **Menu contextuel** : clic droit sur un fichier → "Backups / Versions"
|
||||
- [x] **Coloration syntaxique diff** : lignes ajoutées en vert, supprimées en rouge, en-têtes hunk en accent
|
||||
|
||||
### 7. Fichiers récents par vault
|
||||
- **Effort** : 0.5 jour | **Impact** : 🟡
|
||||
- Dashboard filtré par vault actif
|
||||
|
||||
### 8. Git tags semver
|
||||
- **Effort** : 10 min | **Impact** : 🟢
|
||||
- `git tag v1.7.0 && git push --tags`
|
||||
|
||||
### 9. Filtres search avancés
|
||||
- **Effort** : 1 jour | **Impact** : 🟡
|
||||
- Opérateurs `created:`, `modified:`, `size:`
|
||||
|
||||
### 10. Déduplication IGNORED_DIRS
|
||||
- **Effort** : 0.5 jour | **Impact** : 🟢
|
||||
- `indexer.py` et `watcher.py` ont des définitions séparées → mutualiser
|
||||
|
||||
### ✅ 11. Indicateur AI Actif — FAIT
|
||||
- **Effort** : 0.5 jour | **Impact** : 🟡
|
||||
- Dans le bouton/indicateur à droite du titre, en cliquant il y a un panneau avec les statuts de syncro. Ajouter un indicateur visuel dans ce panneau pour indiquer si l'IA est active, configurée, et fonctionnelle. Durant la phase de traitement de l'IA, le bouton/indicateur devrait changer de couleur pour un blanc qui flash indiquant le statut de la tâche AI en cours.
|
||||
- **Fichiers** : `backend/main.py` (`/api/ai/status`), `frontend/js/ai.js`, `frontend/js/sync.js`, `frontend/style.css`
|
||||
- [x] **Badge header flash blanc** : le `#sync-badge` devient blanc avec animation flash quand une tâche AI est en cours
|
||||
- [x] **Événements** : `ai-processing-start` / `ai-processing-end` dispatchés depuis `ai.js`
|
||||
- [x] **Section IA dans panneau sync** : statut (configurée/non configurée/traitement en cours) avec icône et couleur
|
||||
- [x] **Endpoint `/api/ai/status`** : vérifie si au moins un provider AI est configuré
|
||||
|
||||
### 12. Page d'accueil de voute — FAIT (récursif)
|
||||
- **Effort** : 1 jour | **Impact** : 🟡
|
||||
- Une page d'accueil qui affiche les fichiers de la voute actuelle dans l'ordre du plus récent au plus ancien, récursivement (répertoire + tous les sous-répertoires). Si l'utilisateur change de répertoire dans cette voute, la liste se met à jour automatiquement. ✅
|
||||
- **Fichiers** : `backend/main.py` (`/api/vault/{vault}/files`), `frontend/js/vaulthome.js`, `frontend/js/sidebar.js`, `frontend/style.css`
|
||||
- [x] **Endpoint `/api/vault/{vault}/files?dir=...&recursive=true`** : liste les fichiers d'un répertoire et de tous ses sous-répertoires récursivement (`rglob`), triés par date de modification décroissante
|
||||
- [x] **Champ `rel_dir`** : chaque fichier indique son sous-répertoire relatif pour afficher le chemin dans les résultats
|
||||
- [x] **Affichage style résultats de recherche** : chaque fichier utilise le même look `.search-result-item` (extension, icône, titre, métadonnées avec `sous-dossier/ · date · taille`)
|
||||
- [x] **Badge « récursif »** : indicateur visuel dans le header confirmant le mode récursif
|
||||
- [x] **Breadcrumb navigable** : fil d'Ariane cliquable (vault → dossier → sous-dossier)
|
||||
- [x] **Mise à jour automatique** : quand on clique sur un dossier dans l'arborescence, la page se met à jour
|
||||
- [x] **Déclenchement** : affiché automatiquement quand un vault spécifique est sélectionné
|
||||
|
||||
### ✅ 13. Indexation non-bloquante — FAIT
|
||||
- **Effort** : 0.5 jour | **Impact** : 🟡
|
||||
- L'étape "Building inverted index..." au démarrage bloquait l'event loop asyncio, empêchant le serveur de répondre aux requêtes HTTP (navigation, refresh) jusqu'à la fin de l'indexation.
|
||||
- **Fichiers** : `backend/main.py`, `backend/search.py`
|
||||
- [x] **`init_inverted_index()` dans `run_in_executor`** : exécuté dans le `ThreadPoolExecutor` au lieu de la boucle asyncio principale
|
||||
- [x] **`get_inverted_index()` sans rebuild auto** : ne déclenche plus de rebuild bloquant — retourne l'index vide si pas encore prêt (les recherches retournent 0 résultats jusqu'à la fin de l'indexation)
|
||||
- [x] **Navigation + refresh fonctionnels immédiatement** : le serveur répond aux requêtes HTTP dès le démarrage, même pendant l'indexation
|
||||
|
||||
---
|
||||
|
||||
## 📋 Backlog (P3/P4 — Long terme)
|
||||
|
||||
- **Mode hors-ligne PWA complet** : IndexedDB pour recherche offline + synchro
|
||||
- **OAuth2/OIDC** : Google, GitHub, authentification SSO
|
||||
- **Plugins système** : Extensions utilisateur (custom renderers, search operators)
|
||||
- **Collaboration temps réel** : WebSocket + CRDT (Yjs) pour édition simultanée
|
||||
- **i18n** : Support anglais + français
|
||||
- **MFA** : TOTP/WebAuthn
|
||||
- **Thèmes personnalisés** : CSS variables exposées
|
||||
- **Export multi-formats** : HTML, MD bundle, ePub
|
||||
- **Notifications web** : Push API pour changements de vault
|
||||
- **Health check enrichi** : État de l'index, mémoire, uptime, clients SSE
|
||||
@@ -0,0 +1,262 @@
|
||||
# backend/admin.py
|
||||
# Admin Dashboard endpoints — system stats, audit logs, backup stats,
|
||||
# and Server-Sent Events stream for real-time widgets.
|
||||
#
|
||||
# All endpoints protected with require_admin.
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from backend.audit import AUDIT_LOG_FILE, get_recent_entries
|
||||
from backend.auth.middleware import require_admin
|
||||
from backend.indexer import get_vault_data, index
|
||||
|
||||
logger = logging.getLogger("obsigate.admin")
|
||||
|
||||
router = APIRouter(prefix="/api/admin", tags=["admin"])
|
||||
|
||||
# Server start time — set at module import. Approximates uptime even
|
||||
# if used before lifespan startup fully runs.
|
||||
_SERVER_START_TIME = time.time()
|
||||
|
||||
|
||||
def _server_uptime_seconds() -> float:
|
||||
return time.time() - _SERVER_START_TIME
|
||||
|
||||
|
||||
def _format_timestamp_for_stream(payload: dict) -> str:
|
||||
"""Serialize a payload as an SSE-compatible data line."""
|
||||
return json.dumps(payload, default=str, ensure_ascii=False)
|
||||
|
||||
|
||||
def _get_disk_stats() -> tuple[float, float]:
|
||||
"""Return (used_gb, total_gb) for the data volume or cwd fallback."""
|
||||
# Prefer /data mount when present (production), else use cwd
|
||||
target = "/data" if os.path.isdir("/data") else "."
|
||||
try:
|
||||
usage = shutil.disk_usage(target)
|
||||
used_gb = round(usage.used / (1024 ** 3), 2)
|
||||
total_gb = round(usage.total / (1024 ** 3), 2)
|
||||
return used_gb, total_gb
|
||||
except OSError as e:
|
||||
logger.warning(f"disk_usage failed for {target}: {e}")
|
||||
return 0.0, 0.0
|
||||
|
||||
|
||||
def _count_active_sessions() -> int:
|
||||
"""Count currently-revoked-free active sessions.
|
||||
|
||||
Best-effort estimate: we don't keep an in-memory session store, so we
|
||||
fall back to 1 if the server is up. Useful for the dashboard tile.
|
||||
"""
|
||||
return 1
|
||||
|
||||
|
||||
# ── /api/admin/stats ────────────────────────────────────────────────────
|
||||
@router.get("/stats")
|
||||
async def get_admin_stats(_admin=Depends(require_admin)):
|
||||
"""Return real-time system stats for the admin dashboard."""
|
||||
import psutil
|
||||
|
||||
try:
|
||||
cpu_pct = psutil.cpu_percent(interval=None)
|
||||
except Exception:
|
||||
cpu_pct = 0.0
|
||||
|
||||
try:
|
||||
vm = psutil.virtual_memory()
|
||||
mem_used_mb = round(vm.used / (1024 ** 2), 1)
|
||||
mem_total_mb = round(vm.total / (1024 ** 2), 1)
|
||||
except Exception:
|
||||
mem_used_mb, mem_total_mb = 0.0, 0.0
|
||||
|
||||
disk_used_gb, disk_total_gb = _get_disk_stats()
|
||||
uptime_seconds = int(_server_uptime_seconds())
|
||||
active_sessions = _count_active_sessions()
|
||||
|
||||
return {
|
||||
"cpu_pct": cpu_pct,
|
||||
"mem_used_mb": mem_used_mb,
|
||||
"mem_total_mb": mem_total_mb,
|
||||
"disk_used_gb": disk_used_gb,
|
||||
"disk_total_gb": disk_total_gb,
|
||||
"uptime_seconds": uptime_seconds,
|
||||
"active_sessions": active_sessions,
|
||||
}
|
||||
|
||||
|
||||
# ── /api/admin/audit ───────────────────────────────────────────────────
|
||||
@router.get("/audit")
|
||||
async def get_admin_audit(
|
||||
user: str | None = Query(None, description="Filter by username (substring)"),
|
||||
action: str | None = Query(None, description="Filter by exact action"),
|
||||
limit: int = Query(500, ge=1, le=2000, description="Max entries"),
|
||||
offset: int = Query(0, ge=0, description="Skip first N entries"),
|
||||
_admin=Depends(require_admin),
|
||||
):
|
||||
"""Return recent audit log entries with optional filters."""
|
||||
entries = get_recent_entries(limit=offset + limit, action=action)
|
||||
|
||||
if user:
|
||||
needle = user.lower()
|
||||
entries = [e for e in entries if needle in str(e.get("username", "")).lower()]
|
||||
|
||||
if offset:
|
||||
entries = entries[offset:]
|
||||
|
||||
return {
|
||||
"entries": entries,
|
||||
"total": len(entries),
|
||||
"log_file": str(AUDIT_LOG_FILE),
|
||||
}
|
||||
|
||||
|
||||
# ── /api/admin/backup-stats ───────────────────────────────────────────
|
||||
def _scan_backups() -> list[dict]:
|
||||
"""Walk every vault's backup directory and return one row per .bak file."""
|
||||
rows: list[dict] = []
|
||||
for vault_name in list(index.keys()):
|
||||
vd = get_vault_data(vault_name)
|
||||
if not vd:
|
||||
continue
|
||||
vault_root = Path(vd["path"])
|
||||
backup_root = Path(os.environ.get("OBSIGATE_BACKUP_DIR", ".obsigate-backup"))
|
||||
if not backup_root.is_absolute():
|
||||
backup_root = vault_root / backup_root
|
||||
vault_backup_dir = backup_root / vault_name
|
||||
if not vault_backup_dir.exists():
|
||||
continue
|
||||
try:
|
||||
for fpath in vault_backup_dir.rglob("*.bak"):
|
||||
if not fpath.is_file():
|
||||
continue
|
||||
try:
|
||||
st = fpath.stat()
|
||||
except OSError:
|
||||
continue
|
||||
# Backup filename: {orig}.{timestamp}.bak — split from the right
|
||||
name = fpath.name
|
||||
parts = name.rsplit(".", 2)
|
||||
if len(parts) < 3 or not parts[-2].isdigit():
|
||||
continue
|
||||
try:
|
||||
ts = int(parts[-2])
|
||||
except ValueError:
|
||||
continue
|
||||
rows.append({
|
||||
"vault": vault_name,
|
||||
"filename": name,
|
||||
"timestamp": ts,
|
||||
"size": st.st_size,
|
||||
})
|
||||
except OSError as e:
|
||||
logger.warning(f"Backup scan error in {vault_backup_dir}: {e}")
|
||||
rows.sort(key=lambda r: r["timestamp"], reverse=True)
|
||||
return rows
|
||||
|
||||
|
||||
@router.get("/backup-stats")
|
||||
async def get_admin_backup_stats(_admin=Depends(require_admin)):
|
||||
"""Return backup statistics across all vaults."""
|
||||
rows = _scan_backups()
|
||||
now_ts = int(time.time())
|
||||
total_size_mb = round(sum(r["size"] for r in rows) / (1024 ** 2), 2)
|
||||
|
||||
by_vault: dict[str, dict[str, Any]] = {}
|
||||
for r in rows:
|
||||
bucket = by_vault.setdefault(r["vault"], {"count": 0, "size_mb": 0.0})
|
||||
bucket["count"] += 1
|
||||
bucket["size_mb"] = round(bucket["size_mb"] + r["size"] / (1024 ** 2), 2)
|
||||
|
||||
if rows:
|
||||
newest_ts = rows[0]["timestamp"]
|
||||
oldest_ts = rows[-1]["timestamp"]
|
||||
newest_age_days = round((now_ts - newest_ts) / 86400, 2)
|
||||
oldest_age_days = round((now_ts - oldest_ts) / 86400, 2)
|
||||
else:
|
||||
newest_age_days = 0.0
|
||||
oldest_age_days = 0.0
|
||||
|
||||
return {
|
||||
"total_backups": len(rows),
|
||||
"total_size_mb": total_size_mb,
|
||||
"oldest_age_days": oldest_age_days,
|
||||
"newest_age_days": newest_age_days,
|
||||
"by_vault": by_vault,
|
||||
}
|
||||
|
||||
|
||||
# ── /api/admin/stream — Server-Sent Events ─────────────────────────────
|
||||
async def _stats_event_generator():
|
||||
"""Yield an SSE `stats` event with the current system stats."""
|
||||
try:
|
||||
import psutil
|
||||
# Prime cpu_percent so the first real measurement isn't 0.0
|
||||
psutil.cpu_percent(interval=None)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
last_keepalive = time.time()
|
||||
KEEPALIVE_INTERVAL = 15.0
|
||||
STATS_INTERVAL = 5.0
|
||||
|
||||
try:
|
||||
while True:
|
||||
now = time.time()
|
||||
try:
|
||||
data = await asyncio.to_thread(_build_stats_payload)
|
||||
except Exception as e:
|
||||
logger.warning(f"SSE stats build failed: {e}")
|
||||
data = {"error": str(e)}
|
||||
|
||||
yield f"event: stats\ndata: {_format_timestamp_for_stream(data)}\n\n"
|
||||
|
||||
if now - last_keepalive >= KEEPALIVE_INTERVAL:
|
||||
last_keepalive = now
|
||||
yield ": keepalive\n\n"
|
||||
|
||||
await asyncio.sleep(STATS_INTERVAL)
|
||||
except asyncio.CancelledError:
|
||||
logger.info("SSE stats stream cancelled")
|
||||
raise
|
||||
|
||||
|
||||
def _build_stats_payload() -> dict:
|
||||
"""Synchronous stats builder for the SSE generator."""
|
||||
import psutil
|
||||
cpu_pct = psutil.cpu_percent(interval=None)
|
||||
vm = psutil.virtual_memory()
|
||||
disk_used_gb, disk_total_gb = _get_disk_stats()
|
||||
return {
|
||||
"cpu_pct": cpu_pct,
|
||||
"mem_used_mb": round(vm.used / (1024 ** 2), 1),
|
||||
"mem_total_mb": round(vm.total / (1024 ** 2), 1),
|
||||
"disk_used_gb": disk_used_gb,
|
||||
"disk_total_gb": disk_total_gb,
|
||||
"uptime_seconds": int(_server_uptime_seconds()),
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/stream")
|
||||
async def stream_admin_stats(_admin=Depends(require_admin)):
|
||||
"""Server-Sent Events stream of system metrics, every 5 seconds."""
|
||||
return StreamingResponse(
|
||||
_stats_event_generator(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"X-Accel-Buffering": "no",
|
||||
"Connection": "keep-alive",
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,308 @@
|
||||
"""In-app agent loop — multi-step tool calling.
|
||||
|
||||
The loop drives an LLM that may request tool calls, executes them through the
|
||||
shared tool layer (``backend.tools``), feeds the results back, and repeats
|
||||
until the model produces a final answer or the iteration budget is exhausted.
|
||||
|
||||
The LLM is injected as an async callable so the loop is fully testable without
|
||||
network access::
|
||||
|
||||
async def fake_llm(messages, tools):
|
||||
return LLMResponse(content="done")
|
||||
|
||||
result = await run_agent(messages, ctx=ctx, llm=fake_llm)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
from backend.tools.api import (
|
||||
ToolConfirmationRequired,
|
||||
ToolContext,
|
||||
ToolError,
|
||||
ToolScope,
|
||||
call_tool,
|
||||
get_tool_schemas,
|
||||
)
|
||||
from backend.tools.labels import thought_step_label, tool_step_label
|
||||
|
||||
logger = logging.getLogger("obsigate.agent.loop")
|
||||
|
||||
DEFAULT_MAX_ITERATIONS = 10
|
||||
# Cap the size of a tool result fed back to the model (chars).
|
||||
MAX_TOOL_RESULT_CHARS = 100_000
|
||||
# Quota: maximum tool calls executed per agent run (``BOOKSLM_MAX_TOOL_CALLS``).
|
||||
DEFAULT_MAX_TOOL_CALLS = int(os.environ.get("BOOKSLM_MAX_TOOL_CALLS", "25"))
|
||||
|
||||
# Stopping reasons
|
||||
STOP_DONE = "done"
|
||||
STOP_MAX_ITERATIONS = "max_iterations"
|
||||
STOP_CONFIRMATION_REQUIRED = "confirmation_required"
|
||||
STOP_QUOTA_EXCEEDED = "quota_exceeded"
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolCallRecord:
|
||||
"""Audit-friendly record of one executed tool call."""
|
||||
|
||||
name: str
|
||||
arguments: dict[str, Any]
|
||||
ok: bool
|
||||
result: Any
|
||||
# Human-readable « step » label for the Notion-style UI
|
||||
# ({key, params} — see backend.tools.labels).
|
||||
step: dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentResult:
|
||||
"""Outcome of an agent run."""
|
||||
|
||||
content: str = ""
|
||||
messages: list[dict[str, Any]] = field(default_factory=list)
|
||||
tool_calls: list[ToolCallRecord] = field(default_factory=list)
|
||||
# Ordered Notion-style step descriptors ({key, params}); tool steps and
|
||||
# intermediate reasoning notes interleaved by execution order.
|
||||
steps: list[dict[str, Any]] = field(default_factory=list)
|
||||
iterations: int = 0
|
||||
stopped: str = STOP_DONE
|
||||
pending: dict[str, Any] | None = None
|
||||
|
||||
|
||||
async def provider_llm(messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None):
|
||||
"""Default LLM adapter backed by ``backend.ai_chat.chat_completion``."""
|
||||
from backend.ai_chat import chat_completion
|
||||
|
||||
return await chat_completion(messages, tools=tools)
|
||||
|
||||
|
||||
def _truncate(payload: Any) -> Any:
|
||||
"""Truncate an oversized tool result before feeding it back to the model."""
|
||||
serialized = json.dumps(payload, ensure_ascii=False, default=str)
|
||||
if len(serialized) <= MAX_TOOL_RESULT_CHARS:
|
||||
return payload
|
||||
return {"truncated": True, "content": serialized[:MAX_TOOL_RESULT_CHARS]}
|
||||
|
||||
|
||||
def _assistant_tool_message(content: str | None, tool_calls: list[Any]) -> dict[str, Any]:
|
||||
"""Build the OpenAI-style assistant message carrying tool calls."""
|
||||
return {
|
||||
"role": "assistant",
|
||||
"content": content or "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": call.id,
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": call.name,
|
||||
"arguments": json.dumps(call.arguments, ensure_ascii=False, default=str),
|
||||
},
|
||||
}
|
||||
for call in tool_calls
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _execute_confirmed(
|
||||
ctx: ToolContext,
|
||||
confirm_pending: dict[str, Any],
|
||||
convo: list[dict[str, Any]],
|
||||
executed: list[ToolCallRecord],
|
||||
on_tool_call: Callable[[ToolCallRecord], None] | None,
|
||||
) -> None:
|
||||
"""Apply a previously-paused mutating tool call and feed its result back.
|
||||
|
||||
The pending payload is the ``error`` object emitted by a ``confirmation``
|
||||
event. The assistant tool-call message is expected to already be in
|
||||
``convo`` (it is part of the snapshot returned with the confirmation).
|
||||
"""
|
||||
from backend.ai_chat import ToolCall
|
||||
|
||||
error = confirm_pending.get("error", confirm_pending)
|
||||
name = error.get("tool")
|
||||
arguments = error.get("arguments") or {}
|
||||
call_id = error.get("id") or "call_pending"
|
||||
|
||||
if not name:
|
||||
raise ToolError("Malformed confirmation payload", code="invalid_confirmation")
|
||||
|
||||
# Make sure the assistant tool-call message is present in the snapshot.
|
||||
if not any(
|
||||
m.get("role") == "assistant" and any(
|
||||
tc.get("id") == call_id for tc in (m.get("tool_calls") or [])
|
||||
)
|
||||
for m in convo
|
||||
):
|
||||
convo.append(_assistant_tool_message(None, [ToolCall(id=call_id, name=name, arguments=arguments)]))
|
||||
|
||||
try:
|
||||
result = call_tool(name, ctx, arguments, confirm=True)
|
||||
payload = result.data
|
||||
ok = True
|
||||
except ToolError as e:
|
||||
payload = e.to_dict()
|
||||
ok = False
|
||||
|
||||
record = ToolCallRecord(
|
||||
name=name, arguments=arguments, ok=ok, result=payload,
|
||||
step=tool_step_label(name, arguments),
|
||||
)
|
||||
executed.append(record)
|
||||
if on_tool_call is not None:
|
||||
on_tool_call(record)
|
||||
|
||||
convo.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": call_id,
|
||||
"name": name,
|
||||
"content": json.dumps(_truncate(payload), ensure_ascii=False, default=str),
|
||||
})
|
||||
|
||||
|
||||
async def run_agent(
|
||||
messages: list[dict[str, Any]],
|
||||
*,
|
||||
ctx: ToolContext,
|
||||
llm: Callable[..., Any] | None = None,
|
||||
tools: list[dict[str, Any]] | None = None,
|
||||
max_iterations: int = DEFAULT_MAX_ITERATIONS,
|
||||
max_tool_calls: int | None = None,
|
||||
on_tool_call: Callable[[ToolCallRecord], None] | None = None,
|
||||
on_thought: Callable[[dict[str, Any]], None] | None = None,
|
||||
resume_messages: list[dict[str, Any]] | None = None,
|
||||
confirm_pending: dict[str, Any] | None = None,
|
||||
) -> AgentResult:
|
||||
"""Run the tool-calling loop until completion.
|
||||
|
||||
Args:
|
||||
messages: Initial conversation (OpenAI-style), typically a system
|
||||
message followed by the conversation history and the user message.
|
||||
ctx: Tool execution context (identity, mode, confirmation state).
|
||||
llm: Async callable ``(messages, tools) -> LLMResponse``. Defaults to
|
||||
the real provider adapter.
|
||||
tools: Tool schemas to expose. ``None`` exposes all in-app tools;
|
||||
pass ``[]`` to disable tool calling (plain chat).
|
||||
max_iterations: Hard cap on LLM round-trips.
|
||||
max_tool_calls: Hard cap on the total number of executed tool calls
|
||||
(quota, defaults to ``BOOKSLM_MAX_TOOL_CALLS``).
|
||||
on_tool_call: Optional callback invoked after each executed tool call.
|
||||
resume_messages: Conversation snapshot from a paused run (returned with
|
||||
a ``confirmation`` event). When set, the loop resumes from it.
|
||||
confirm_pending: Pending mutating tool call to apply before resuming
|
||||
(two-step propose/apply).
|
||||
|
||||
Returns:
|
||||
An :class:`AgentResult`. ``stopped`` is ``done``, ``max_iterations`` or
|
||||
``confirmation_required`` (in which case ``pending`` holds the payload
|
||||
to confirm, for the two-step propose/apply flow).
|
||||
"""
|
||||
llm = llm or provider_llm
|
||||
if tools is None:
|
||||
tools = get_tool_schemas(scope=ToolScope.IN_APP)
|
||||
quota = DEFAULT_MAX_TOOL_CALLS if max_tool_calls is None else max_tool_calls
|
||||
steps: list[dict[str, Any]] = []
|
||||
|
||||
def _emit_note(text: str) -> None:
|
||||
"""Record an intermediate reasoning note as a visible step."""
|
||||
note = thought_step_label(text)
|
||||
if note["params"]["value"]:
|
||||
steps.append(note)
|
||||
if on_thought is not None:
|
||||
on_thought(note)
|
||||
|
||||
convo = [dict(m) for m in (resume_messages if resume_messages is not None else messages)]
|
||||
executed: list[ToolCallRecord] = []
|
||||
|
||||
if confirm_pending:
|
||||
if quota is not None and len(executed) >= quota:
|
||||
return AgentResult(
|
||||
content="",
|
||||
messages=convo,
|
||||
tool_calls=executed,
|
||||
steps=steps,
|
||||
iterations=0,
|
||||
stopped=STOP_QUOTA_EXCEEDED,
|
||||
)
|
||||
_execute_confirmed(ctx, confirm_pending, convo, executed, on_tool_call)
|
||||
|
||||
for iteration in range(1, max_iterations + 1):
|
||||
response = await llm(convo, tools)
|
||||
|
||||
if not response.has_tool_calls:
|
||||
return AgentResult(
|
||||
content=response.content or "",
|
||||
messages=convo,
|
||||
tool_calls=executed,
|
||||
steps=steps,
|
||||
iterations=iteration,
|
||||
stopped=STOP_DONE,
|
||||
)
|
||||
|
||||
# Intermediate reasoning shown alongside tool calls → a "thought" step.
|
||||
_emit_note(response.content or "")
|
||||
convo.append(_assistant_tool_message(response.content, response.tool_calls))
|
||||
|
||||
for call in response.tool_calls:
|
||||
if quota is not None and len(executed) >= quota:
|
||||
logger.warning(f"Agent reached the tool-call quota ({quota})")
|
||||
return AgentResult(
|
||||
content=response.content or "",
|
||||
messages=convo,
|
||||
tool_calls=executed,
|
||||
steps=steps,
|
||||
iterations=iteration,
|
||||
stopped=STOP_QUOTA_EXCEEDED,
|
||||
)
|
||||
try:
|
||||
result = call_tool(call.name, ctx, call.arguments)
|
||||
payload = result.data
|
||||
ok = True
|
||||
except ToolConfirmationRequired as e:
|
||||
logger.info(f"Agent paused: confirmation required for '{call.name}'")
|
||||
pending = e.to_dict()
|
||||
# Include the tool-call id so the client can echo it back.
|
||||
pending["error"]["id"] = call.id
|
||||
return AgentResult(
|
||||
content=response.content or "",
|
||||
messages=convo,
|
||||
tool_calls=executed,
|
||||
steps=steps,
|
||||
iterations=iteration,
|
||||
stopped=STOP_CONFIRMATION_REQUIRED,
|
||||
pending=pending,
|
||||
)
|
||||
except ToolError as e:
|
||||
payload = e.to_dict()
|
||||
ok = False
|
||||
|
||||
record = ToolCallRecord(
|
||||
name=call.name, arguments=call.arguments, ok=ok, result=payload,
|
||||
step=tool_step_label(call.name, call.arguments),
|
||||
)
|
||||
executed.append(record)
|
||||
steps.append(record.step)
|
||||
if on_tool_call is not None:
|
||||
on_tool_call(record)
|
||||
|
||||
convo.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": call.id,
|
||||
"name": call.name,
|
||||
"content": json.dumps(_truncate(payload), ensure_ascii=False, default=str),
|
||||
})
|
||||
|
||||
logger.warning(f"Agent reached max iterations ({max_iterations})")
|
||||
return AgentResult(
|
||||
content="",
|
||||
messages=convo,
|
||||
tool_calls=executed,
|
||||
steps=steps,
|
||||
iterations=max_iterations,
|
||||
stopped=STOP_MAX_ITERATIONS,
|
||||
)
|
||||
+163
-51
@@ -1,49 +1,146 @@
|
||||
"""ObsiGate AI — Multi-provider AI service for editor enhancement.
|
||||
|
||||
Supports: DeepSeek, OpenRouter, Google Gemini.
|
||||
Supports: DeepSeek, OpenRouter, Google Gemini, Ollama, NVIDIA, QwenCloud, Xiaomi, Mistral.
|
||||
Configured via environment variables.
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
import httpx
|
||||
from typing import Optional, Literal
|
||||
|
||||
logger = logging.getLogger("obsigate.ai")
|
||||
|
||||
ProviderName = Literal["deepseek", "openrouter", "gemini"]
|
||||
ProviderName = Literal["deepseek", "openrouter", "gemini", "ollama", "nvidia", "qwencloud", "xiaomi", "mistral"]
|
||||
|
||||
# Provider configurations
|
||||
PROVIDERS = {
|
||||
"deepseek": {
|
||||
"api_key": os.getenv("DEEPSEEK_API_KEY", "").strip(),
|
||||
"base_url": "https://api.deepseek.com/v1",
|
||||
"model": os.getenv("DEEPSEEK_MODEL", "deepseek-chat"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"openrouter": {
|
||||
"api_key": os.getenv("OPENROUTER_API_KEY", "").strip(),
|
||||
"base_url": "https://openrouter.ai/api/v1",
|
||||
"model": os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"gemini": {
|
||||
"api_key": os.getenv("GEMINI_API_KEY", "").strip(),
|
||||
"base_url": "https://generativelanguage.googleapis.com/v1beta",
|
||||
"model": os.getenv("GEMINI_MODEL", "gemini-2.0-flash"),
|
||||
"auth_header": None, # Uses query param ?key=
|
||||
},
|
||||
}
|
||||
# Provider configurations — keys loaded from file or .env
|
||||
AI_KEYS_FILE = Path("data/api_keys.json")
|
||||
APP_CONFIG_FILE = Path(__file__).resolve().parent.parent / "data" / "config.json"
|
||||
|
||||
def _read_ai_keys() -> dict:
|
||||
if not AI_KEYS_FILE.exists():
|
||||
return {}
|
||||
try:
|
||||
return json.loads(AI_KEYS_FILE.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def _read_app_config() -> dict:
|
||||
"""Read the persisted application config (``data/config.json``)."""
|
||||
if not APP_CONFIG_FILE.exists():
|
||||
return {}
|
||||
try:
|
||||
return json.loads(APP_CONFIG_FILE.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
def get_ai_key(env_name: str) -> str:
|
||||
"""Get AI key: stored file first, then .env fallback."""
|
||||
keys = _read_ai_keys()
|
||||
if keys.get(env_name):
|
||||
return keys[env_name]
|
||||
return os.getenv(env_name, "").strip()
|
||||
|
||||
def _load_provider_keys():
|
||||
"""Load AI keys from stored file, falling back to .env."""
|
||||
providers = {
|
||||
"deepseek": {
|
||||
"api_key": get_ai_key("DEEPSEEK_API_KEY"),
|
||||
"base_url": "https://api.deepseek.com/v1",
|
||||
"model": os.getenv("DEEPSEEK_MODEL", "deepseek-chat"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"openrouter": {
|
||||
"api_key": get_ai_key("OPENROUTER_API_KEY"),
|
||||
"base_url": "https://openrouter.ai/api/v1",
|
||||
"model": os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"gemini": {
|
||||
"api_key": get_ai_key("GEMINI_API_KEY"),
|
||||
"base_url": "https://generativelanguage.googleapis.com/v1beta",
|
||||
"model": os.getenv("GEMINI_MODEL", "gemini-2.0-flash"),
|
||||
"auth_header": None,
|
||||
},
|
||||
"ollama": {
|
||||
"api_key": "ollama",
|
||||
"base_url": os.getenv("OLLAMA_BASE_URL", "http://ollama:11434/v1"),
|
||||
"model": os.getenv("OLLAMA_MODEL", "qwen2.5-coder:1.5b"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"nvidia": {
|
||||
"api_key": get_ai_key("NVIDIA_API_KEY"),
|
||||
"base_url": "https://integrate.api.nvidia.com/v1",
|
||||
"model": os.getenv("NVIDIA_MODEL", "meta/llama-3.1-405b-instruct"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"qwencloud": {
|
||||
"api_key": get_ai_key("QWENCLOUD_API_KEY"),
|
||||
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||
"model": os.getenv("QWENCLOUD_MODEL", "qwen-max"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
"xiaomi": {
|
||||
"api_key": get_ai_key("XIAOMI_API_KEY"),
|
||||
"base_url": os.getenv("XIAOMI_BASE_URL", "https://api.xiaomimimo.com/v1"),
|
||||
"model": os.getenv("XIAOMI_MODEL", "mimo-v2.5-pro"),
|
||||
# Xiaomi MiMo uses a dedicated `api-key` header (NOT Authorization: Bearer).
|
||||
# The `_call_deepseek_openrouter` helper substitutes {api_key} verbatim,
|
||||
# so we just emit the raw key value here.
|
||||
"auth_header": "{api_key}",
|
||||
"auth_header_name": "api-key",
|
||||
},
|
||||
"mistral": {
|
||||
"api_key": get_ai_key("MISTRAL_API_KEY"),
|
||||
"base_url": "https://api.mistral.ai/v1",
|
||||
"model": os.getenv("MISTRAL_MODEL", "mistral-large-latest"),
|
||||
"auth_header": "Bearer {api_key}",
|
||||
},
|
||||
}
|
||||
# Apply persisted per-provider model overrides (data/config.json).
|
||||
overrides = _read_app_config().get("ai_default_models") or {}
|
||||
if isinstance(overrides, dict):
|
||||
for name, model in overrides.items():
|
||||
if name in providers and isinstance(model, str) and model:
|
||||
providers[name]["model"] = model
|
||||
return providers
|
||||
|
||||
|
||||
PROVIDERS = _load_provider_keys()
|
||||
|
||||
DEFAULT_PROVIDER: ProviderName = os.getenv("AI_DEFAULT_PROVIDER", "deepseek") # type: ignore
|
||||
|
||||
|
||||
def _get_provider_config(provider: Optional[ProviderName] = None) -> dict:
|
||||
def get_default_provider() -> str:
|
||||
"""Resolve the default provider: ``data/config.json`` > env > ``deepseek``."""
|
||||
provider: str = str(_read_app_config().get("ai_default_provider") or os.getenv("AI_DEFAULT_PROVIDER", "deepseek"))
|
||||
return provider if provider in PROVIDERS else "deepseek"
|
||||
|
||||
|
||||
def reload_ai_config() -> str:
|
||||
"""Reload provider keys and model overrides from disk into ``PROVIDERS`` in place.
|
||||
|
||||
Mutating ``PROVIDERS`` (rather than rebinding it) keeps references held by
|
||||
other modules valid. Returns the resolved default provider.
|
||||
"""
|
||||
global DEFAULT_PROVIDER
|
||||
PROVIDERS.clear()
|
||||
PROVIDERS.update(_load_provider_keys())
|
||||
DEFAULT_PROVIDER = get_default_provider() # type: ignore[assignment]
|
||||
logger.info(f"AI config reloaded (default provider: {DEFAULT_PROVIDER})")
|
||||
return DEFAULT_PROVIDER
|
||||
|
||||
|
||||
def _get_provider_config(provider: ProviderName | None = None) -> dict:
|
||||
"""Get provider config, falling back to default if requested provider unavailable."""
|
||||
p = provider or DEFAULT_PROVIDER
|
||||
default = get_default_provider()
|
||||
p = provider or default
|
||||
if p not in PROVIDERS:
|
||||
p = DEFAULT_PROVIDER
|
||||
p = default
|
||||
cfg = PROVIDERS[p]
|
||||
if not cfg["api_key"]:
|
||||
# Try next available provider
|
||||
@@ -55,17 +152,31 @@ def _get_provider_config(provider: Optional[ProviderName] = None) -> dict:
|
||||
return {"name": p, **cfg}
|
||||
|
||||
|
||||
async def _call_deepseek_openrouter(prompt: str, system: str, provider: Optional[ProviderName] = None,
|
||||
def _build_headers(cfg: dict) -> dict:
|
||||
"""Build HTTP headers for an OpenAI-compatible provider config.
|
||||
|
||||
Most providers use ``Authorization: Bearer KEY``. Some (Xiaomi MiMo) use a
|
||||
dedicated header like ``api-key: KEY`` — supported via the
|
||||
``auth_header_name`` key in PROVIDERS (defaults to ``Authorization``).
|
||||
"""
|
||||
header_name = cfg.get("auth_header_name") or "Authorization"
|
||||
header_value = cfg["auth_header"].format(api_key=cfg["api_key"])
|
||||
if header_name == "Authorization" and not header_value.lower().startswith("bearer "):
|
||||
header_value = "Bearer " + header_value
|
||||
return {
|
||||
header_name: header_value,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
async def _call_deepseek_openrouter(prompt: str, system: str, provider: ProviderName | None = None,
|
||||
temperature: float = 0.7, max_tokens: int = 2048) -> str:
|
||||
"""Call OpenAI-compatible API (DeepSeek, OpenRouter)."""
|
||||
"""Call OpenAI-compatible API (DeepSeek, OpenRouter, Xiaomi MiMo, etc.)."""
|
||||
cfg = _get_provider_config(provider)
|
||||
# Debug: log masked key to diagnose 401
|
||||
key_preview = cfg["api_key"][:8] + "..." + cfg["api_key"][-4:] if len(cfg["api_key"]) > 12 else "***"
|
||||
logger.info(f"AI call: provider={cfg['name']} model={cfg['model']} key={key_preview}")
|
||||
headers = {
|
||||
"Authorization": cfg["auth_header"].format(api_key=cfg["api_key"]),
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
headers = _build_headers(cfg)
|
||||
payload = {
|
||||
"model": cfg["model"],
|
||||
"messages": [
|
||||
@@ -105,11 +216,12 @@ async def _call_gemini(prompt: str, system: str, temperature: float = 0.7, max_t
|
||||
return data["candidates"][0]["content"]["parts"][0]["text"].strip()
|
||||
|
||||
|
||||
async def ai_complete(prompt: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_complete(prompt: str, provider: ProviderName | None = None) -> str:
|
||||
"""Generic AI completion. Routes to appropriate provider."""
|
||||
cfg = _get_provider_config(provider)
|
||||
if cfg["name"] == "gemini":
|
||||
return await _call_gemini(prompt, "You are a helpful assistant.")
|
||||
# All other providers use OpenAI-compatible format
|
||||
return await _call_deepseek_openrouter(prompt, "You are a helpful assistant.", provider)
|
||||
|
||||
|
||||
@@ -120,7 +232,7 @@ Your responses should be direct and concise. When editing text, return ONLY the
|
||||
no explanations or markdown fences."""
|
||||
|
||||
|
||||
async def ai_improve_writing(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_improve_writing(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Improve writing quality while preserving meaning."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Improve the following text. Fix grammar, clarity, and flow. Preserve the original language and meaning.\n\n{text}",
|
||||
@@ -128,7 +240,7 @@ async def ai_improve_writing(text: str, provider: Optional[ProviderName] = None)
|
||||
)
|
||||
|
||||
|
||||
async def ai_fix_spelling(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_fix_spelling(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Fix spelling and grammar errors."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Fix all spelling and grammar errors in this text. Return only the corrected text.\n\n{text}",
|
||||
@@ -136,7 +248,7 @@ async def ai_fix_spelling(text: str, provider: Optional[ProviderName] = None) ->
|
||||
)
|
||||
|
||||
|
||||
async def ai_make_shorter(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_make_shorter(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Make text more concise."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Make this text shorter and more concise while preserving the key information.\n\n{text}",
|
||||
@@ -144,7 +256,7 @@ async def ai_make_shorter(text: str, provider: Optional[ProviderName] = None) ->
|
||||
)
|
||||
|
||||
|
||||
async def ai_make_longer(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_make_longer(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Expand text with more detail."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Expand this text with more detail, examples, or explanation while keeping the same tone.\n\n{text}",
|
||||
@@ -152,7 +264,7 @@ async def ai_make_longer(text: str, provider: Optional[ProviderName] = None) ->
|
||||
)
|
||||
|
||||
|
||||
async def ai_simplify(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_simplify(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Simplify language."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Simplify this text. Use clearer, more straightforward language. Avoid jargon.\n\n{text}",
|
||||
@@ -160,7 +272,7 @@ async def ai_simplify(text: str, provider: Optional[ProviderName] = None) -> str
|
||||
)
|
||||
|
||||
|
||||
async def ai_change_tone(text: str, tone: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_change_tone(text: str, tone: str, provider: ProviderName | None = None) -> str:
|
||||
"""Change the tone of the text."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Rewrite this text in a {tone} tone. Preserve the original meaning.\n\n{text}",
|
||||
@@ -168,7 +280,7 @@ async def ai_change_tone(text: str, tone: str, provider: Optional[ProviderName]
|
||||
)
|
||||
|
||||
|
||||
async def ai_translate(text: str, target_lang: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_translate(text: str, target_lang: str, provider: ProviderName | None = None) -> str:
|
||||
"""Translate text to target language."""
|
||||
# Gemini is better at translation
|
||||
if provider is None and PROVIDERS["gemini"]["api_key"]:
|
||||
@@ -184,7 +296,7 @@ async def ai_translate(text: str, target_lang: str, provider: Optional[ProviderN
|
||||
)
|
||||
|
||||
|
||||
async def ai_explain(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_explain(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Explain the selected text."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Explain the following text clearly and concisely:\n\n{text}",
|
||||
@@ -192,7 +304,7 @@ async def ai_explain(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
)
|
||||
|
||||
|
||||
async def ai_summarize(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_summarize(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Summarize the selected text."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Summarize the following text concisely:\n\n{text}",
|
||||
@@ -200,7 +312,7 @@ async def ai_summarize(text: str, provider: Optional[ProviderName] = None) -> st
|
||||
)
|
||||
|
||||
|
||||
async def ai_continue_writing(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_continue_writing(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Continue writing from the selected text."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Continue writing from where this text leaves off. Match the style and tone:\n\n{text}",
|
||||
@@ -208,7 +320,7 @@ async def ai_continue_writing(text: str, provider: Optional[ProviderName] = None
|
||||
)
|
||||
|
||||
|
||||
async def ai_custom_rewrite(text: str, instruction: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_custom_rewrite(text: str, instruction: str, provider: ProviderName | None = None) -> str:
|
||||
"""Rewrite text based on a custom instruction."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Rewrite the following text according to this instruction: {instruction}\n\n{text}",
|
||||
@@ -216,7 +328,7 @@ async def ai_custom_rewrite(text: str, instruction: str, provider: Optional[Prov
|
||||
)
|
||||
|
||||
|
||||
async def ai_convert_to_list(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_convert_to_list(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Convert paragraph text to a markdown list."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Convert this text into a well-organized markdown bullet list. Extract key points.\n\n{text}",
|
||||
@@ -224,7 +336,7 @@ async def ai_convert_to_list(text: str, provider: Optional[ProviderName] = None)
|
||||
)
|
||||
|
||||
|
||||
async def ai_convert_to_table(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_convert_to_table(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Convert text to a markdown table."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Convert this information into a markdown table. Choose appropriate columns.\n\n{text}",
|
||||
@@ -232,7 +344,7 @@ async def ai_convert_to_table(text: str, provider: Optional[ProviderName] = None
|
||||
)
|
||||
|
||||
|
||||
async def ai_generate_frontmatter(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_generate_frontmatter(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Generate YAML frontmatter for a markdown document."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Generate YAML frontmatter for this markdown document. Include: titre, tags (as list), catégorie, statut, date. Return ONLY the YAML between --- markers.\n\n{text}",
|
||||
@@ -240,7 +352,7 @@ async def ai_generate_frontmatter(text: str, provider: Optional[ProviderName] =
|
||||
)
|
||||
|
||||
|
||||
async def ai_inline_complete(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_inline_complete(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Inline completion — suggest continuation."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Complete this text naturally. Return only the completion (just the new text, no repetition):\n\n{text}",
|
||||
@@ -248,7 +360,7 @@ async def ai_inline_complete(text: str, provider: Optional[ProviderName] = None)
|
||||
)
|
||||
|
||||
|
||||
async def ai_convert_to_canvas(text: str, provider: Optional[ProviderName] = None) -> str:
|
||||
async def ai_convert_to_canvas(text: str, provider: ProviderName | None = None) -> str:
|
||||
"""Convert text to a Mermaid diagram or canvas representation."""
|
||||
return await _call_deepseek_openrouter(
|
||||
f"Convert this content into a Mermaid.js diagram if applicable, or a structured outline. Choose the best format.\n\n{text}",
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
"""Provider-agnostic chat completion with native tool (function) calling.
|
||||
|
||||
This module complements ``backend.ai`` (which handles the 16 stateless editor
|
||||
actions). It exposes a single ``chat_completion`` entry point used by the
|
||||
in-app agent loop:
|
||||
|
||||
- OpenAI-compatible providers (DeepSeek, OpenRouter, NVIDIA, QwenCloud,
|
||||
Xiaomi, Mistral) use the ``tools`` / ``tool_calls`` protocol.
|
||||
- Google Gemini uses ``functionDeclarations`` / ``functionCall``.
|
||||
|
||||
When a provider rejects the ``tools`` parameter (model without function
|
||||
calling support), the call is transparently retried without tools so callers
|
||||
degrade to plain chat (fallback protocol, see ``docs/AI_ARCHITECTURE_GUIDE.md``).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import AsyncIterator
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from backend.ai import PROVIDERS, _build_headers, _get_provider_config
|
||||
|
||||
logger = logging.getLogger("obsigate.ai_chat")
|
||||
|
||||
# Status codes that usually mean "tools not supported by this model".
|
||||
_TOOLS_UNSUPPORTED_STATUS = {400, 404, 422}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolCall:
|
||||
"""A single tool invocation requested by the model."""
|
||||
|
||||
id: str
|
||||
name: str
|
||||
arguments: dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass
|
||||
class LLMResponse:
|
||||
"""Normalized provider response (text and/or tool calls)."""
|
||||
|
||||
content: str | None = None
|
||||
tool_calls: list[ToolCall] = field(default_factory=list)
|
||||
provider: str = ""
|
||||
model: str = ""
|
||||
|
||||
@property
|
||||
def has_tool_calls(self) -> bool:
|
||||
return bool(self.tool_calls)
|
||||
|
||||
|
||||
def _parse_arguments(raw: Any) -> dict[str, Any]:
|
||||
"""Parse tool-call arguments that may arrive as a JSON string or dict."""
|
||||
if isinstance(raw, dict):
|
||||
return raw
|
||||
if isinstance(raw, str) and raw.strip():
|
||||
try:
|
||||
parsed = json.loads(raw)
|
||||
return parsed if isinstance(parsed, dict) else {"value": parsed}
|
||||
except json.JSONDecodeError:
|
||||
return {"_raw": raw}
|
||||
return {}
|
||||
|
||||
|
||||
async def chat_completion(
|
||||
messages: list[dict[str, Any]],
|
||||
*,
|
||||
tools: list[dict[str, Any]] | None = None,
|
||||
provider: str | None = None,
|
||||
model: str | None = None,
|
||||
temperature: float = 0.3,
|
||||
max_tokens: int = 4096,
|
||||
) -> LLMResponse:
|
||||
"""Run a chat completion, optionally with tool calling.
|
||||
|
||||
Args:
|
||||
messages: OpenAI-style messages (``system`` / ``user`` / ``assistant`` /
|
||||
``tool``). Assistant messages may carry ``tool_calls``.
|
||||
tools: OpenAI-style tool schemas (see ``get_tool_schemas``).
|
||||
provider: Provider override; falls back to the configured default.
|
||||
model: Model override; falls back to the provider's default model.
|
||||
temperature: Sampling temperature.
|
||||
max_tokens: Maximum output tokens.
|
||||
|
||||
Returns:
|
||||
:class:`LLMResponse` with the text content and/or requested tool calls.
|
||||
"""
|
||||
cfg = _get_provider_config(provider) # type: ignore[arg-type]
|
||||
name = cfg["name"]
|
||||
resolved_model = model or cfg["model"]
|
||||
|
||||
if name == "gemini":
|
||||
return await _gemini_chat(messages, tools, resolved_model, temperature, max_tokens)
|
||||
return await _openai_chat(messages, tools, cfg, resolved_model, temperature, max_tokens)
|
||||
|
||||
|
||||
async def _openai_chat(
|
||||
messages: list[dict[str, Any]],
|
||||
tools: list[dict[str, Any]] | None,
|
||||
cfg: dict[str, Any],
|
||||
model: str,
|
||||
temperature: float,
|
||||
max_tokens: int,
|
||||
) -> LLMResponse:
|
||||
"""Call an OpenAI-compatible ``/chat/completions`` endpoint."""
|
||||
headers = _build_headers(cfg)
|
||||
url = f"{cfg['base_url']}/chat/completions"
|
||||
|
||||
def _build_payload(use_tools: list[dict[str, Any]] | None) -> dict[str, Any]:
|
||||
payload: dict[str, Any] = {
|
||||
"model": model,
|
||||
"messages": messages,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens,
|
||||
}
|
||||
if use_tools:
|
||||
payload["tools"] = use_tools
|
||||
payload["tool_choice"] = "auto"
|
||||
return payload
|
||||
|
||||
try:
|
||||
data = await _post_json(url, headers, _build_payload(tools))
|
||||
except httpx.HTTPStatusError as e:
|
||||
if tools and e.response.status_code in _TOOLS_UNSUPPORTED_STATUS:
|
||||
logger.warning(f"Provider '{cfg['name']}' rejected tools — retrying without tools")
|
||||
data = await _post_json(url, headers, _build_payload(None))
|
||||
else:
|
||||
raise
|
||||
|
||||
message = data["choices"][0]["message"]
|
||||
content = (message.get("content") or "").strip() or None
|
||||
|
||||
tool_calls: list[ToolCall] = []
|
||||
for idx, tc in enumerate(message.get("tool_calls") or []):
|
||||
fn = tc.get("function", {}) or {}
|
||||
tool_calls.append(ToolCall(
|
||||
id=tc.get("id") or f"call_{idx}",
|
||||
name=fn.get("name", ""),
|
||||
arguments=_parse_arguments(fn.get("arguments")),
|
||||
))
|
||||
|
||||
return LLMResponse(content=content, tool_calls=tool_calls, provider=cfg["name"], model=model)
|
||||
|
||||
|
||||
def _gemini_tools(tools: list[dict[str, Any]] | None) -> list[dict[str, Any]] | None:
|
||||
"""Convert OpenAI-style tool schemas to Gemini ``functionDeclarations``."""
|
||||
if not tools:
|
||||
return None
|
||||
declarations = []
|
||||
for spec in tools:
|
||||
fn = spec.get("function", spec)
|
||||
declarations.append({
|
||||
"name": fn.get("name", ""),
|
||||
"description": fn.get("description", ""),
|
||||
"parameters": fn.get("parameters", {"type": "object", "properties": {}}),
|
||||
})
|
||||
return [{"functionDeclarations": declarations}]
|
||||
|
||||
|
||||
_DATA_URL_RE = re.compile(r"^data:([^;,]+);base64,(.*)$", re.DOTALL)
|
||||
|
||||
|
||||
def _content_to_gemini_parts(content: Any) -> list[dict[str, Any]]:
|
||||
"""Convert OpenAI-style message content to Gemini ``parts``.
|
||||
|
||||
Accepts either a plain string or a multimodal content array
|
||||
(``[{"type": "text", ...}, {"type": "image_url", ...}]``). Data URLs are
|
||||
turned into ``inlineData`` parts so images can be sent to vision models.
|
||||
"""
|
||||
if content is None:
|
||||
return [{"text": ""}]
|
||||
if isinstance(content, str):
|
||||
return [{"text": content}]
|
||||
|
||||
parts: list[dict[str, Any]] = []
|
||||
if isinstance(content, list):
|
||||
for item in content:
|
||||
if isinstance(item, str):
|
||||
parts.append({"text": item})
|
||||
continue
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
item_type = item.get("type")
|
||||
if item_type == "text":
|
||||
parts.append({"text": item.get("text", "")})
|
||||
elif item_type == "image_url":
|
||||
url = (item.get("image_url") or {}).get("url", "")
|
||||
match = _DATA_URL_RE.match(url or "")
|
||||
if match:
|
||||
parts.append({
|
||||
"inlineData": {"mimeType": match.group(1), "data": match.group(2)},
|
||||
})
|
||||
elif url:
|
||||
parts.append({"fileData": {"fileUri": url}})
|
||||
if not parts:
|
||||
parts.append({"text": ""})
|
||||
return parts
|
||||
|
||||
|
||||
def _gemini_contents(messages: list[dict[str, Any]]) -> tuple[str, list[dict[str, Any]]]:
|
||||
"""Split OpenAI-style messages into Gemini ``system`` text + ``contents``."""
|
||||
system_parts: list[str] = []
|
||||
contents: list[dict[str, Any]] = []
|
||||
for msg in messages:
|
||||
role = msg.get("role")
|
||||
if role == "system":
|
||||
content = msg.get("content") or ""
|
||||
system_parts.append(content if isinstance(content, str) else "")
|
||||
elif role == "tool":
|
||||
contents.append({
|
||||
"role": "user",
|
||||
"parts": [{
|
||||
"functionResponse": {
|
||||
"name": msg.get("name", ""),
|
||||
"response": {"content": msg.get("content", "")},
|
||||
},
|
||||
}],
|
||||
})
|
||||
else:
|
||||
contents.append({
|
||||
"role": "user" if role == "user" else "model",
|
||||
"parts": _content_to_gemini_parts(msg.get("content")),
|
||||
})
|
||||
return "\n".join(p for p in system_parts if p).strip(), contents
|
||||
|
||||
|
||||
async def _gemini_chat(
|
||||
messages: list[dict[str, Any]],
|
||||
tools: list[dict[str, Any]] | None,
|
||||
model: str,
|
||||
temperature: float,
|
||||
max_tokens: int,
|
||||
) -> LLMResponse:
|
||||
"""Call Gemini's ``generateContent`` endpoint with optional tools."""
|
||||
cfg = PROVIDERS["gemini"]
|
||||
system, contents = _gemini_contents(messages)
|
||||
payload: dict[str, Any] = {
|
||||
"contents": contents,
|
||||
"generationConfig": {"temperature": temperature, "maxOutputTokens": max_tokens},
|
||||
}
|
||||
if system:
|
||||
payload["system_instruction"] = {"parts": [{"text": system}]}
|
||||
gemini_tools = _gemini_tools(tools)
|
||||
if gemini_tools:
|
||||
payload["tools"] = gemini_tools
|
||||
|
||||
url = f"{cfg['base_url']}/models/{model}:generateContent?key={cfg['api_key']}"
|
||||
data = await _post_json(url, None, payload)
|
||||
|
||||
parts = data["candidates"][0]["content"].get("parts", [])
|
||||
content = "".join(p.get("text", "") for p in parts if "text" in p).strip() or None
|
||||
|
||||
tool_calls: list[ToolCall] = []
|
||||
for idx, part in enumerate(parts):
|
||||
fc = part.get("functionCall")
|
||||
if fc:
|
||||
tool_calls.append(ToolCall(
|
||||
id=f"call_{idx}",
|
||||
name=fc.get("name", ""),
|
||||
arguments=_parse_arguments(fc.get("args")),
|
||||
))
|
||||
|
||||
return LLMResponse(content=content, tool_calls=tool_calls, provider="gemini", model=model)
|
||||
|
||||
|
||||
async def _post_json(url: str, headers: dict[str, str] | None, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""POST JSON and return the parsed body, raising on HTTP errors."""
|
||||
async with httpx.AsyncClient(timeout=120.0) as client:
|
||||
resp = await client.post(url, headers=headers or {}, json=payload)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
# ── Streaming (SSE token stream) ────────────────────────────────────────
|
||||
|
||||
|
||||
async def stream_completion(
|
||||
messages: list[dict[str, Any]],
|
||||
*,
|
||||
provider: str | None = None,
|
||||
model: str | None = None,
|
||||
temperature: float = 0.3,
|
||||
max_tokens: int = 4096,
|
||||
) -> AsyncIterator[str]:
|
||||
"""Yield content deltas from a chat completion as they arrive.
|
||||
|
||||
Only text content is streamed (no tool calling): this backs the plain
|
||||
``/api/ai/bookslm/chat`` endpoint. The tool-calling ``/agent`` endpoint
|
||||
keeps using :func:`chat_completion` because tool calls need the complete
|
||||
response before they can be executed.
|
||||
"""
|
||||
cfg = _get_provider_config(provider) # type: ignore[arg-type]
|
||||
resolved_model = model or cfg["model"]
|
||||
|
||||
if cfg["name"] == "gemini":
|
||||
stream = _gemini_stream(messages, resolved_model, temperature, max_tokens)
|
||||
else:
|
||||
stream = _openai_stream(messages, cfg, resolved_model, temperature, max_tokens)
|
||||
|
||||
async for chunk in stream:
|
||||
yield chunk
|
||||
|
||||
|
||||
async def _openai_stream(
|
||||
messages: list[dict[str, Any]],
|
||||
cfg: dict[str, Any],
|
||||
model: str,
|
||||
temperature: float,
|
||||
max_tokens: int,
|
||||
) -> AsyncIterator[str]:
|
||||
"""Stream an OpenAI-compatible ``/chat/completions`` response."""
|
||||
headers = _build_headers(cfg)
|
||||
url = f"{cfg['base_url']}/chat/completions"
|
||||
payload: dict[str, Any] = {
|
||||
"model": model,
|
||||
"messages": messages,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens,
|
||||
"stream": True,
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(timeout=120.0) as client, client.stream("POST", url, headers=headers, json=payload) as resp:
|
||||
resp.raise_for_status()
|
||||
async for line in resp.aiter_lines():
|
||||
if not line or not line.startswith("data:"):
|
||||
continue
|
||||
raw = line[5:].strip()
|
||||
if raw == "[DONE]":
|
||||
break
|
||||
try:
|
||||
chunk = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
choices = chunk.get("choices") or []
|
||||
if not choices:
|
||||
continue
|
||||
delta = choices[0].get("delta") or {}
|
||||
content = delta.get("content")
|
||||
if content:
|
||||
yield content
|
||||
|
||||
|
||||
async def _gemini_stream(
|
||||
messages: list[dict[str, Any]],
|
||||
model: str,
|
||||
temperature: float,
|
||||
max_tokens: int,
|
||||
) -> AsyncIterator[str]:
|
||||
"""Stream Gemini's ``streamGenerateContent`` response (SSE)."""
|
||||
cfg = PROVIDERS["gemini"]
|
||||
system, contents = _gemini_contents(messages)
|
||||
payload: dict[str, Any] = {
|
||||
"contents": contents,
|
||||
"generationConfig": {"temperature": temperature, "maxOutputTokens": max_tokens},
|
||||
}
|
||||
if system:
|
||||
payload["system_instruction"] = {"parts": [{"text": system}]}
|
||||
|
||||
url = f"{cfg['base_url']}/models/{model}:streamGenerateContent?alt=sse&key={cfg['api_key']}"
|
||||
async with httpx.AsyncClient(timeout=120.0) as client, client.stream("POST", url, json=payload) as resp:
|
||||
resp.raise_for_status()
|
||||
async for line in resp.aiter_lines():
|
||||
if not line or not line.startswith("data:"):
|
||||
continue
|
||||
raw = line[5:].strip()
|
||||
if not raw:
|
||||
continue
|
||||
try:
|
||||
chunk = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
for candidate in chunk.get("candidates") or []:
|
||||
for part in candidate.get("content", {}).get("parts", []):
|
||||
text = part.get("text")
|
||||
if text:
|
||||
yield text
|
||||
@@ -0,0 +1,175 @@
|
||||
# backend/ai_history.py
|
||||
"""Persistent assistant conversation history (#95).
|
||||
|
||||
Each authenticated user owns a flat list of conversation sessions tagged with
|
||||
their context (mode / vault / directory / documents). Sessions survive page
|
||||
reloads and are the source of truth for the panel history menu (#96 sidebar
|
||||
"Historique IA" reads the same store).
|
||||
|
||||
Format of a session (JS/JSON shape kept identical to the client, minus
|
||||
transient fields):
|
||||
|
||||
{
|
||||
"id": "s-…",
|
||||
"title": "…",
|
||||
"mode": "directory" | "documents" | "general",
|
||||
"vault": "…" | None,
|
||||
"directory": "…" | "",
|
||||
"documents": [{"vault": "…", "path": "…"}],
|
||||
"context": "directory-…", # _contextKey() of the assistant
|
||||
"createdAt": 1234567890,
|
||||
"updatedAt": 1234567890,
|
||||
"messages": [{"role": "user|assistant", "content": "…"}]
|
||||
}
|
||||
|
||||
Sessions are capped per user (see MAX_SESSIONS); the oldest ones are dropped
|
||||
when the cap is reached.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.ai_history")
|
||||
|
||||
AI_HISTORY_DIR = Path("data/ai_history")
|
||||
MAX_SESSIONS = 200
|
||||
|
||||
|
||||
def _get_user_file(username: str) -> Path:
|
||||
AI_HISTORY_DIR.mkdir(parents=True, exist_ok=True)
|
||||
return AI_HISTORY_DIR / f"{username}.json"
|
||||
|
||||
|
||||
def _read_sessions(username: str) -> list[dict[str, Any]]:
|
||||
path = _get_user_file(username)
|
||||
if not path.exists():
|
||||
return []
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
except Exception as e: # pragma: no cover - defensive I/O guard
|
||||
logger.error(f"Failed to read AI history for {username}: {e}")
|
||||
return []
|
||||
if not isinstance(data, list):
|
||||
return []
|
||||
return [s for s in data if isinstance(s, dict)]
|
||||
|
||||
|
||||
def _write_sessions(username: str, sessions: list[dict[str, Any]]) -> None:
|
||||
path = _get_user_file(username)
|
||||
try:
|
||||
tmp = path.with_suffix(".tmp")
|
||||
tmp.write_text(
|
||||
json.dumps(sessions, indent=2, ensure_ascii=False),
|
||||
encoding="utf-8",
|
||||
)
|
||||
shutil.move(str(tmp), str(path))
|
||||
except Exception as e: # pragma: no cover - defensive I/O guard
|
||||
logger.error(f"Failed to write AI history for {username}: {e}")
|
||||
|
||||
|
||||
def _summary(session: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Compact representation (no messages) used by the list endpoint."""
|
||||
messages = session.get("messages") or []
|
||||
preview = ""
|
||||
for msg in reversed(messages):
|
||||
content = (msg.get("content") or "").strip() if isinstance(msg, dict) else ""
|
||||
if content:
|
||||
preview = content[:120]
|
||||
break
|
||||
return {
|
||||
"id": session.get("id", ""),
|
||||
"title": session.get("title", "") or "",
|
||||
"mode": session.get("mode", "general"),
|
||||
"vault": session.get("vault"),
|
||||
"directory": session.get("directory", ""),
|
||||
"context": session.get("context", ""),
|
||||
"createdAt": session.get("createdAt", 0),
|
||||
"updatedAt": session.get("updatedAt", session.get("createdAt", 0)),
|
||||
"message_count": len(messages),
|
||||
"preview": preview,
|
||||
}
|
||||
|
||||
|
||||
def list_sessions(username: str, *, include_messages: bool = False) -> list[dict[str, Any]]:
|
||||
"""Return the user's sessions, most recently updated first.
|
||||
|
||||
With ``include_messages=False`` (default) a compact summary is returned;
|
||||
the full conversation is fetched per id via :func:`get_session`.
|
||||
"""
|
||||
if not username:
|
||||
return []
|
||||
sessions = sorted(
|
||||
_read_sessions(username),
|
||||
key=lambda s: s.get("updatedAt") or s.get("createdAt") or 0,
|
||||
reverse=True,
|
||||
)
|
||||
if include_messages:
|
||||
return sessions
|
||||
return [_summary(s) for s in sessions]
|
||||
|
||||
|
||||
def get_session(username: str, session_id: str) -> dict[str, Any] | None:
|
||||
if not username or not session_id:
|
||||
return None
|
||||
for session in _read_sessions(username):
|
||||
if session.get("id") == session_id:
|
||||
return session
|
||||
return None
|
||||
|
||||
|
||||
def upsert_session(username: str, session: dict[str, Any]) -> dict[str, Any] | None:
|
||||
"""Create or update a conversation for the user.
|
||||
|
||||
Returns the stored session, or None when there is no valid id.
|
||||
"""
|
||||
if not username:
|
||||
return None
|
||||
session_id = (session.get("id") or "").strip()
|
||||
if not session_id:
|
||||
return None
|
||||
|
||||
now = session.get("updatedAt") or session.get("createdAt") or 0
|
||||
stored = {
|
||||
"id": session_id,
|
||||
"title": session.get("title", "") or "",
|
||||
"mode": session.get("mode") or "general",
|
||||
"vault": session.get("vault"),
|
||||
"directory": session.get("directory", ""),
|
||||
"documents": session.get("documents") or [],
|
||||
"context": session.get("context", ""),
|
||||
"createdAt": session.get("createdAt") or now,
|
||||
"updatedAt": now,
|
||||
"messages": session.get("messages") or [],
|
||||
}
|
||||
|
||||
sessions = _read_sessions(username)
|
||||
replaced = False
|
||||
for i, existing in enumerate(sessions):
|
||||
if existing.get("id") == session_id:
|
||||
sessions[i] = stored
|
||||
replaced = True
|
||||
break
|
||||
if not replaced:
|
||||
sessions.append(stored)
|
||||
|
||||
sessions.sort(key=lambda s: s.get("updatedAt") or s.get("createdAt") or 0, reverse=True)
|
||||
if len(sessions) > MAX_SESSIONS:
|
||||
logger.info(f"AI history cap reached for {username}: trimming to {MAX_SESSIONS}")
|
||||
sessions = sessions[:MAX_SESSIONS]
|
||||
|
||||
_write_sessions(username, sessions)
|
||||
return stored
|
||||
|
||||
|
||||
def delete_session(username: str, session_id: str) -> bool:
|
||||
if not username or not session_id:
|
||||
return False
|
||||
sessions = _read_sessions(username)
|
||||
remaining = [s for s in sessions if s.get("id") != session_id]
|
||||
if len(remaining) == len(sessions):
|
||||
return False
|
||||
_write_sessions(username, remaining)
|
||||
return True
|
||||
+115
-17
@@ -1,55 +1,111 @@
|
||||
"""ObsiGate AI — API routes for AI-powered editor features."""
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.ai import (
|
||||
ai_improve_writing, ai_fix_spelling, ai_make_shorter, ai_make_longer,
|
||||
ai_simplify, ai_change_tone, ai_translate, ai_explain, ai_summarize,
|
||||
ai_continue_writing, ai_custom_rewrite, ai_convert_to_list,
|
||||
ai_convert_to_table, ai_generate_frontmatter, ai_inline_complete,
|
||||
ai_convert_to_canvas, DEFAULT_PROVIDER, PROVIDERS,
|
||||
PROVIDERS,
|
||||
ai_change_tone,
|
||||
ai_continue_writing,
|
||||
ai_convert_to_canvas,
|
||||
ai_convert_to_list,
|
||||
ai_convert_to_table,
|
||||
ai_custom_rewrite,
|
||||
ai_explain,
|
||||
ai_fix_spelling,
|
||||
ai_generate_frontmatter,
|
||||
ai_improve_writing,
|
||||
ai_inline_complete,
|
||||
ai_make_longer,
|
||||
ai_make_shorter,
|
||||
ai_simplify,
|
||||
ai_summarize,
|
||||
ai_translate,
|
||||
get_default_provider,
|
||||
)
|
||||
from backend.auth.middleware import require_auth
|
||||
from backend.model_capabilities import get_model_capabilities
|
||||
from backend.schemas import AIStatusResponse
|
||||
|
||||
logger = logging.getLogger("obsigate.ai_routes")
|
||||
router = APIRouter(prefix="/api/ai", tags=["AI"])
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
@router.get("/status", response_model=AIStatusResponse)
|
||||
async def api_status():
|
||||
"""Check if AI is configured and which providers are available.
|
||||
|
||||
Reads environment variables at runtime so that changes to
|
||||
.env or Docker environment are reflected immediately.
|
||||
"""
|
||||
import os as _os
|
||||
from backend.ai import get_ai_key
|
||||
provider_keys = {
|
||||
"deepseek": "DEEPSEEK_API_KEY",
|
||||
"openrouter": "OPENROUTER_API_KEY",
|
||||
"gemini": "GEMINI_API_KEY",
|
||||
"nvidia": "NVIDIA_API_KEY",
|
||||
"qwencloud": "QWENCLOUD_API_KEY",
|
||||
"xiaomi": "XIAOMI_API_KEY",
|
||||
"mistral": "MISTRAL_API_KEY",
|
||||
}
|
||||
providers = {}
|
||||
for name, env_var in provider_keys.items():
|
||||
has_key = bool(_os.getenv(env_var, "").strip())
|
||||
has_key = bool(get_ai_key(env_var))
|
||||
providers[name] = {
|
||||
"available": has_key,
|
||||
"model": PROVIDERS[name]["model"] if has_key else None,
|
||||
}
|
||||
|
||||
# ---- Autocomplete (Ollama) status ----
|
||||
ollama_cfg = PROVIDERS.get("ollama", {})
|
||||
ollama_url = (ollama_cfg.get("base_url") or "").rstrip("/v1").rstrip("/")
|
||||
ollama_model = ollama_cfg.get("model", "")
|
||||
autocomplete = {
|
||||
"available": False,
|
||||
"server_ok": False,
|
||||
"model_loaded": False,
|
||||
"model": ollama_model,
|
||||
"error": None,
|
||||
}
|
||||
if ollama_url:
|
||||
try:
|
||||
import httpx
|
||||
async with httpx.AsyncClient(timeout=5.0) as client:
|
||||
# Check server health + list loaded models
|
||||
r = await client.get(f"{ollama_url}/api/tags")
|
||||
if r.status_code == 200:
|
||||
autocomplete["server_ok"] = True
|
||||
data = r.json()
|
||||
models = [m.get("name", "") for m in data.get("models", [])]
|
||||
# Check if our model (with or without tag) is loaded
|
||||
model_base = ollama_model.split(":")[0]
|
||||
autocomplete["model_loaded"] = any(
|
||||
m == ollama_model or m.startswith(model_base + ":")
|
||||
for m in models
|
||||
)
|
||||
autocomplete["available"] = autocomplete["model_loaded"]
|
||||
else:
|
||||
autocomplete["error"] = f"HTTP {r.status_code}"
|
||||
except Exception as e:
|
||||
autocomplete["error"] = str(e)
|
||||
|
||||
return {
|
||||
"configured": any(p["available"] for p in providers.values()),
|
||||
"default_provider": DEFAULT_PROVIDER,
|
||||
"default_provider": get_default_provider(),
|
||||
"providers": providers,
|
||||
"autocomplete": autocomplete,
|
||||
}
|
||||
|
||||
|
||||
class AIRequest(BaseModel):
|
||||
text: str = Field(..., description="Input text to process", min_length=1)
|
||||
instruction: Optional[str] = Field(None, description="Custom instruction for rewrite")
|
||||
target_lang: Optional[str] = Field(None, description="Target language for translation")
|
||||
tone: Optional[str] = Field(None, description="Target tone (professional, casual, etc.)")
|
||||
provider: Optional[str] = Field(None, description="AI provider override")
|
||||
instruction: str | None = Field(None, description="Custom instruction for rewrite")
|
||||
target_lang: str | None = Field(None, description="Target language for translation")
|
||||
tone: str | None = Field(None, description="Target tone (professional, casual, etc.)")
|
||||
provider: str | None = Field(None, description="AI provider override (e.g. 'deepseek', 'nvidia')")
|
||||
model: str | None = Field(None, description="Model name override for this request")
|
||||
|
||||
|
||||
class AIResponse(BaseModel):
|
||||
@@ -59,6 +115,12 @@ class AIResponse(BaseModel):
|
||||
|
||||
async def _handle(action, request: AIRequest):
|
||||
"""Wrapper with error handling."""
|
||||
from backend.ai import PROVIDERS
|
||||
# Apply per-request model override (saved/restored around the call)
|
||||
original_model = None
|
||||
if request.model and request.provider and request.provider in PROVIDERS:
|
||||
original_model = PROVIDERS[request.provider].get("model")
|
||||
PROVIDERS[request.provider]["model"] = request.model
|
||||
try:
|
||||
result = await action(request.text, request.provider)
|
||||
return AIResponse(result=result, provider=request.provider or "default")
|
||||
@@ -66,7 +128,10 @@ async def _handle(action, request: AIRequest):
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"AI error: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"AI service error: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"AI service error: {e!s}")
|
||||
finally:
|
||||
if original_model is not None and request.provider in PROVIDERS:
|
||||
PROVIDERS[request.provider]["model"] = original_model
|
||||
|
||||
|
||||
@router.post("/improve", response_model=AIResponse)
|
||||
@@ -169,3 +234,36 @@ async def api_inline_complete(req: AIRequest):
|
||||
async def api_to_canvas(req: AIRequest):
|
||||
"""Convert to Mermaid diagram or outline."""
|
||||
return await _handle(ai_convert_to_canvas, req)
|
||||
|
||||
|
||||
class ModelCapabilitiesResponse(BaseModel):
|
||||
"""Capabilities of a single provider/model pair."""
|
||||
|
||||
provider: str
|
||||
model: str
|
||||
capabilities: dict[str, bool] = Field(
|
||||
description="Flags: chat, embeddings, rerank, images, video, "
|
||||
"audio_speech, audio_transcription, vision",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/model-capabilities", response_model=ModelCapabilitiesResponse)
|
||||
async def api_model_capabilities(
|
||||
provider: str = Query(..., description="Provider identifier"),
|
||||
model: str = Query("", description="Model identifier (optional)"),
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Return the capability flags for a provider/model pair.
|
||||
|
||||
Two layers (BUG-044): flags the provider itself declares in its models
|
||||
endpoint (Mistral ``capabilities``, OpenRouter ``architecture``) win, the
|
||||
curated table in ``backend.model_capabilities`` fills the rest. The
|
||||
declaration snapshot is populated by ``GET /api/config/ai-models``; when it
|
||||
is cold (or the provider declares nothing) the curated table answers alone,
|
||||
so this endpoint never performs a blocking provider call.
|
||||
"""
|
||||
return {
|
||||
"provider": provider,
|
||||
"model": model,
|
||||
"capabilities": get_model_capabilities(provider, model),
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.attachment_indexer")
|
||||
|
||||
@@ -10,16 +10,16 @@ logger = logging.getLogger("obsigate.attachment_indexer")
|
||||
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".bmp", ".ico"}
|
||||
|
||||
# Global attachment index: {vault_name: {filename_lower: [absolute_path, ...]}}
|
||||
attachment_index: Dict[str, Dict[str, List[Path]]] = {}
|
||||
attachment_index: dict[str, dict[str, list[Path]]] = {}
|
||||
|
||||
# Resolution cache: {(vault_name, filename): resolved_path}
|
||||
_resolution_cache: Dict[tuple, Optional[Path]] = {}
|
||||
_resolution_cache: dict[tuple, Path | None] = {}
|
||||
|
||||
# Thread-safe lock for index updates
|
||||
_attachment_lock = threading.Lock()
|
||||
|
||||
|
||||
def clear_resolution_cache(vault_name: Optional[str] = None) -> None:
|
||||
def clear_resolution_cache(vault_name: str | None = None) -> None:
|
||||
"""Clear the resolution cache for a specific vault or all vaults.
|
||||
|
||||
Args:
|
||||
@@ -29,12 +29,12 @@ def clear_resolution_cache(vault_name: Optional[str] = None) -> None:
|
||||
if vault_name is None:
|
||||
_resolution_cache.clear()
|
||||
else:
|
||||
keys_to_remove = [k for k in _resolution_cache.keys() if k[0] == vault_name]
|
||||
keys_to_remove = [k for k in _resolution_cache if k[0] == vault_name]
|
||||
for key in keys_to_remove:
|
||||
del _resolution_cache[key]
|
||||
|
||||
|
||||
def _scan_vault_attachments(vault_name: str, vault_path: str, vault_cfg: dict | None = None) -> Dict[str, List[Path]]:
|
||||
def _scan_vault_attachments(vault_name: str, vault_path: str, vault_cfg: dict | None = None) -> dict[str, list[Path]]:
|
||||
"""Synchronously scan a vault directory for image attachments.
|
||||
|
||||
Walks the vault tree and builds a filename -> absolute path mapping
|
||||
@@ -49,7 +49,7 @@ def _scan_vault_attachments(vault_name: str, vault_path: str, vault_cfg: dict |
|
||||
Dict mapping lowercase filenames to lists of absolute paths.
|
||||
"""
|
||||
vault_root = Path(vault_path)
|
||||
index: Dict[str, List[Path]] = {}
|
||||
index: dict[str, list[Path]] = {}
|
||||
|
||||
if not vault_root.exists():
|
||||
logger.warning(f"Vault path does not exist for attachment scan: {vault_path}")
|
||||
@@ -84,7 +84,7 @@ def _scan_vault_attachments(vault_name: str, vault_path: str, vault_cfg: dict |
|
||||
return index
|
||||
|
||||
|
||||
async def build_attachment_index(vault_config: Dict[str, Dict[str, Any]]) -> None:
|
||||
async def build_attachment_index(vault_config: dict[str, dict[str, Any]]) -> None:
|
||||
"""Build the attachment index for all configured vaults.
|
||||
|
||||
Runs vault scans concurrently in a thread pool, then performs
|
||||
@@ -101,9 +101,9 @@ async def build_attachment_index(vault_config: Dict[str, Dict[str, Any]]) -> Non
|
||||
return
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
new_index: Dict[str, Dict[str, List[Path]]] = {}
|
||||
new_index: dict[str, dict[str, list[Path]]] = {}
|
||||
|
||||
tasks: list[tuple[str, asyncio.Future[Dict[str, List[Path]]]]] = []
|
||||
tasks: list[tuple[str, asyncio.Future[dict[str, list[Path]]]]] = []
|
||||
for name, config in vault_config.items():
|
||||
vault_path = config.get("path")
|
||||
if not vault_path:
|
||||
@@ -159,9 +159,9 @@ def resolve_image_path(
|
||||
image_src: str,
|
||||
vault_name: str,
|
||||
vault_root: Path,
|
||||
current_file_path: Optional[Path] = None,
|
||||
attachments_path: Optional[str] = None
|
||||
) -> Optional[Path]:
|
||||
current_file_path: Path | None = None,
|
||||
attachments_path: str | None = None
|
||||
) -> Path | None:
|
||||
"""Resolve an image source path using multi-strategy resolution.
|
||||
|
||||
Applies 7 resolution strategies in priority order:
|
||||
@@ -193,8 +193,8 @@ def resolve_image_path(
|
||||
resolved = None
|
||||
|
||||
# Strategy 1: Absolute path
|
||||
if src_path.is_absolute():
|
||||
if src_path.exists() and src_path.is_file():
|
||||
if src_path.is_absolute() and src_path.exists() and src_path.is_file():
|
||||
|
||||
resolved = src_path
|
||||
logger.debug(f"Image resolved via strategy 1 (absolute): {image_src}")
|
||||
|
||||
@@ -279,7 +279,7 @@ def resolve_image_path(
|
||||
return resolved
|
||||
|
||||
|
||||
def get_attachment_stats(vault_name: Optional[str] = None) -> Dict[str, int]:
|
||||
def get_attachment_stats(vault_name: str | None = None) -> dict[str, int]:
|
||||
"""Get attachment statistics for a vault or all vaults.
|
||||
|
||||
Args:
|
||||
|
||||
+7
-8
@@ -8,11 +8,10 @@ Format: JSON lines (one JSON object per line) for easy parsing.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
logger = logging.getLogger("obsigate.audit")
|
||||
|
||||
@@ -51,7 +50,7 @@ def log_file_save(
|
||||
vault_name: str,
|
||||
file_path: str,
|
||||
size: int,
|
||||
ip: Optional[str] = None,
|
||||
ip: str | None = None,
|
||||
):
|
||||
"""Log a file save (PUT) operation."""
|
||||
_write_entry({
|
||||
@@ -69,7 +68,7 @@ def log_file_delete(
|
||||
username: str,
|
||||
vault_name: str,
|
||||
file_path: str,
|
||||
ip: Optional[str] = None,
|
||||
ip: str | None = None,
|
||||
):
|
||||
"""Log a file delete operation."""
|
||||
_write_entry({
|
||||
@@ -85,7 +84,7 @@ def log_file_delete(
|
||||
def log_config_change(
|
||||
username: str,
|
||||
changes: dict,
|
||||
ip: Optional[str] = None,
|
||||
ip: str | None = None,
|
||||
):
|
||||
"""Log a configuration change."""
|
||||
_write_entry({
|
||||
@@ -97,7 +96,7 @@ def log_config_change(
|
||||
})
|
||||
|
||||
|
||||
def log_vault_add(username: str, vault_name: str, vault_path: str, ip: Optional[str] = None):
|
||||
def log_vault_add(username: str, vault_name: str, vault_path: str, ip: str | None = None):
|
||||
"""Log a vault addition."""
|
||||
_write_entry({
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
@@ -109,7 +108,7 @@ def log_vault_add(username: str, vault_name: str, vault_path: str, ip: Optional[
|
||||
})
|
||||
|
||||
|
||||
def log_vault_remove(username: str, vault_name: str, ip: Optional[str] = None):
|
||||
def log_vault_remove(username: str, vault_name: str, ip: str | None = None):
|
||||
"""Log a vault removal."""
|
||||
_write_entry({
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
@@ -120,7 +119,7 @@ def log_vault_remove(username: str, vault_name: str, ip: Optional[str] = None):
|
||||
})
|
||||
|
||||
|
||||
def get_recent_entries(limit: int = 100, action: Optional[str] = None) -> list:
|
||||
def get_recent_entries(limit: int = 100, action: str | None = None) -> list:
|
||||
"""Read the most recent audit log entries.
|
||||
|
||||
Args:
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
# Revoked token JTIs persisted to data/revoked_tokens.json.
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import uuid
|
||||
import time
|
||||
import logging
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from jose import jwt, JWTError
|
||||
from typing import Optional
|
||||
|
||||
from jose import JWTError, jwt
|
||||
|
||||
logger = logging.getLogger("obsigate.auth.jwt")
|
||||
|
||||
@@ -62,21 +62,26 @@ def create_access_token(user: dict) -> str:
|
||||
return jwt.encode(payload, get_secret_key(), algorithm=ALGORITHM)
|
||||
|
||||
|
||||
def create_refresh_token(username: str) -> tuple:
|
||||
"""Create a JWT refresh token. Returns (token_string, jti)."""
|
||||
def create_refresh_token(username: str, remember: bool = False) -> tuple:
|
||||
"""Create a JWT refresh token. Returns (token_string, jti).
|
||||
|
||||
``remember`` is carried as a claim so token rotation can preserve the
|
||||
30-day vs 7-day lifetime chosen at login.
|
||||
"""
|
||||
now = int(time.time())
|
||||
jti = str(uuid.uuid4())
|
||||
payload = {
|
||||
"sub": username,
|
||||
"jti": jti,
|
||||
"iat": now,
|
||||
"exp": now + REFRESH_TOKEN_EXPIRE_SECONDS,
|
||||
"exp": now + (2592000 if remember else REFRESH_TOKEN_EXPIRE_SECONDS),
|
||||
"type": "refresh",
|
||||
"remember": remember,
|
||||
}
|
||||
return jwt.encode(payload, get_secret_key(), algorithm=ALGORITHM), jti
|
||||
|
||||
|
||||
def decode_token(token: str) -> Optional[dict]:
|
||||
def decode_token(token: str) -> dict | None:
|
||||
"""Decode and validate a JWT. Returns None if invalid/expired."""
|
||||
try:
|
||||
return jwt.decode(token, get_secret_key(), algorithms=[ALGORITHM])
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
# backend/auth/mfa.py
|
||||
# Multi-Factor Authentication: TOTP + recovery codes.
|
||||
# TOTP via pyotp, recovery codes hashed with argon2 for single-use storage.
|
||||
|
||||
import hashlib
|
||||
import logging
|
||||
import secrets
|
||||
|
||||
import pyotp
|
||||
|
||||
logger = logging.getLogger("obsigate.auth.mfa")
|
||||
|
||||
TOTP_ISSUER = "ObsiGate"
|
||||
|
||||
|
||||
def generate_secret() -> str:
|
||||
"""Generate a new TOTP secret (base32-encoded, 160 bits)."""
|
||||
return pyotp.random_base32()
|
||||
|
||||
|
||||
def generate_qr_uri(secret: str, username: str, issuer: str = TOTP_ISSUER) -> str:
|
||||
"""Generate an otpauth:// URI for QR code generation."""
|
||||
totp = pyotp.TOTP(secret)
|
||||
return totp.provisioning_uri(name=username, issuer_name=issuer)
|
||||
|
||||
|
||||
def verify_totp(secret: str, code: str) -> bool:
|
||||
"""Verify a TOTP code with a ±1 window tolerance."""
|
||||
totp = pyotp.TOTP(secret)
|
||||
return totp.verify(code, valid_window=1)
|
||||
|
||||
|
||||
def generate_recovery_codes(n: int = 8) -> list[str]:
|
||||
"""Generate n human-readable recovery codes (XXXX-XXXX format)."""
|
||||
codes = []
|
||||
for _ in range(n):
|
||||
# 8 chars alphanumeric, grouped with dash for readability
|
||||
raw = secrets.token_hex(4).upper()
|
||||
code = f"{raw[:4]}-{raw[4:]}"
|
||||
codes.append(code)
|
||||
return codes
|
||||
|
||||
|
||||
def hash_recovery_code(code: str) -> str:
|
||||
"""Hash a recovery code for storage (SHA-256 for fast comparison).
|
||||
|
||||
We use SHA-256 instead of argon2 here because recovery codes are
|
||||
high-entropy random strings, not user-chosen passwords.
|
||||
"""
|
||||
return hashlib.sha256(code.upper().encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def verify_recovery_code(code: str, hashed_codes: list[str]) -> int | None:
|
||||
"""Verify a recovery code against stored hashes.
|
||||
|
||||
Returns the index of the matched code (for removal), or None if invalid.
|
||||
Comparison is case-insensitive.
|
||||
"""
|
||||
code_hash = hash_recovery_code(code)
|
||||
for i, stored_hash in enumerate(hashed_codes):
|
||||
if secrets.compare_digest(code_hash, stored_hash):
|
||||
return i
|
||||
return None
|
||||
@@ -2,13 +2,15 @@
|
||||
# FastAPI dependencies for authentication and authorization.
|
||||
# Reads JWT from Authorization header OR access_token cookie.
|
||||
|
||||
import os
|
||||
import logging
|
||||
from fastapi import Request, HTTPException, Depends
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from typing import Optional
|
||||
import os
|
||||
|
||||
from .jwt_handler import decode_token
|
||||
from fastapi import Depends, HTTPException, Request
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from backend.services.net import get_client_ip
|
||||
|
||||
from .jwt_handler import decode_token, is_token_revoked
|
||||
from .user_store import get_user
|
||||
|
||||
logger = logging.getLogger("obsigate.auth.middleware")
|
||||
@@ -26,8 +28,8 @@ def is_auth_enabled() -> bool:
|
||||
|
||||
def get_current_user(
|
||||
request: Request,
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security),
|
||||
) -> Optional[dict]:
|
||||
credentials: HTTPAuthorizationCredentials | None = Depends(security),
|
||||
) -> dict | None:
|
||||
"""Extract and validate the current user from JWT.
|
||||
|
||||
Reads token from Authorization header first, falls back to access_token cookie.
|
||||
@@ -42,6 +44,7 @@ def get_current_user(
|
||||
"vaults": ["*"],
|
||||
"active": True,
|
||||
"_token_vaults": ["*"],
|
||||
"_request_ip": get_client_ip(request),
|
||||
}
|
||||
|
||||
token = None
|
||||
@@ -57,12 +60,31 @@ def get_current_user(
|
||||
if not payload or payload.get("type") != "access":
|
||||
return None
|
||||
|
||||
# BUG-027: access tokens revoked at logout must be rejected immediately.
|
||||
jti = payload.get("jti")
|
||||
if jti and is_token_revoked(jti):
|
||||
return None
|
||||
|
||||
user = get_user(payload["sub"])
|
||||
if not user or not user.get("active"):
|
||||
return None
|
||||
|
||||
# BUG-028: a password change invalidates every token issued before it.
|
||||
pca = user.get("password_changed_at")
|
||||
iat = payload.get("iat")
|
||||
if pca is not None and iat is not None:
|
||||
try:
|
||||
if int(iat) < int(float(pca)):
|
||||
return None
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
# Attach vault permissions from the token (snapshot at login time)
|
||||
user["_token_vaults"] = payload.get("vaults", [])
|
||||
# Attach the token id for per-token rate limiting (AI tool layer).
|
||||
user["_token_jti"] = payload.get("jti")
|
||||
# BUG-030: expose the real client IP to the audit log.
|
||||
user["_request_ip"] = get_client_ip(request)
|
||||
return user
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# Parameters: time_cost=2, memory_cost=64MB, parallelism=2
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerifyMismatchError, VerificationError
|
||||
from argon2.exceptions import VerificationError, VerifyMismatchError
|
||||
|
||||
ph = PasswordHasher(
|
||||
time_cost=2,
|
||||
@@ -13,6 +13,32 @@ ph = PasswordHasher(
|
||||
salt_len=16,
|
||||
)
|
||||
|
||||
# Password policy (BUG-028). Applied by the API validators at account creation
|
||||
# and password change so the rules stay consistent across both paths.
|
||||
MIN_PASSWORD_LENGTH = 8
|
||||
MAX_PASSWORD_LENGTH = 128
|
||||
|
||||
|
||||
def validate_password_strength(password: str) -> str:
|
||||
"""Validate a plaintext password against the project policy.
|
||||
|
||||
Args:
|
||||
password: Candidate password.
|
||||
|
||||
Returns:
|
||||
The password unchanged when valid.
|
||||
|
||||
Raises:
|
||||
ValueError: When the password is too short, too long or blank.
|
||||
"""
|
||||
if password is None or len(password) < MIN_PASSWORD_LENGTH:
|
||||
raise ValueError(f"Minimum {MIN_PASSWORD_LENGTH} caractères")
|
||||
if len(password) > MAX_PASSWORD_LENGTH:
|
||||
raise ValueError(f"Maximum {MAX_PASSWORD_LENGTH} caractères")
|
||||
if not password.strip():
|
||||
raise ValueError("Le mot de passe ne peut pas être vide")
|
||||
return password
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
"""Hash a password with Argon2id."""
|
||||
|
||||
+596
-43
@@ -2,24 +2,48 @@
|
||||
# All /api/auth/* endpoints: login, logout, refresh, me, change-password,
|
||||
# and admin user CRUD.
|
||||
|
||||
import re
|
||||
import logging
|
||||
from fastapi import APIRouter, HTTPException, Response, Request, Depends
|
||||
import re
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, HTTPException, Request, Response
|
||||
from pydantic import BaseModel, validator
|
||||
from typing import List, Optional
|
||||
|
||||
from backend.ratelimit import is_account_rate_limited, is_rate_limited
|
||||
from backend.ratelimit import record_account_failure as rl_record_account_failure
|
||||
from backend.ratelimit import record_account_success as rl_record_account_success
|
||||
from backend.ratelimit import record_failure as rl_record_failure
|
||||
from backend.ratelimit import record_success as rl_record_success
|
||||
from backend.services.net import get_client_ip
|
||||
|
||||
from .user_store import (
|
||||
get_user, get_all_users, create_user, update_user, delete_user,
|
||||
record_login_success, record_login_failure, is_locked, has_users,
|
||||
)
|
||||
from .jwt_handler import (
|
||||
create_access_token, create_refresh_token, decode_token,
|
||||
revoke_token, is_token_revoked,
|
||||
ACCESS_TOKEN_EXPIRE_SECONDS,
|
||||
create_access_token,
|
||||
create_refresh_token,
|
||||
decode_token,
|
||||
is_token_revoked,
|
||||
revoke_token,
|
||||
)
|
||||
from .mfa import (
|
||||
generate_qr_uri,
|
||||
generate_recovery_codes,
|
||||
generate_secret,
|
||||
hash_recovery_code,
|
||||
verify_recovery_code,
|
||||
verify_totp,
|
||||
)
|
||||
from .middleware import is_auth_enabled, require_admin, require_auth
|
||||
from .password import hash_password, validate_password_strength, verify_password
|
||||
from .user_store import (
|
||||
create_user,
|
||||
delete_user,
|
||||
get_all_users,
|
||||
get_user,
|
||||
has_users,
|
||||
is_locked,
|
||||
record_login_failure,
|
||||
record_login_success,
|
||||
update_user,
|
||||
)
|
||||
from .middleware import require_auth, require_admin, is_auth_enabled
|
||||
from .password import verify_password, hash_password
|
||||
from backend.ratelimit import is_rate_limited, record_failure as rl_record_failure, record_success as rl_record_success
|
||||
|
||||
logger = logging.getLogger("obsigate.auth.router")
|
||||
|
||||
@@ -40,17 +64,19 @@ class ChangePasswordRequest(BaseModel):
|
||||
|
||||
@validator("new_password")
|
||||
def password_strength(cls, v):
|
||||
if len(v) < 8:
|
||||
raise ValueError("Minimum 8 caractères")
|
||||
return v
|
||||
return validate_password_strength(v)
|
||||
|
||||
|
||||
class CreateUserRequest(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
display_name: Optional[str] = None
|
||||
display_name: str | None = None
|
||||
role: str = "user"
|
||||
vaults: List[str] = []
|
||||
vaults: list[str] = []
|
||||
|
||||
@validator("password")
|
||||
def password_valid(cls, v):
|
||||
return validate_password_strength(v)
|
||||
|
||||
@validator("username")
|
||||
def username_valid(cls, v):
|
||||
@@ -66,11 +92,17 @@ class CreateUserRequest(BaseModel):
|
||||
|
||||
|
||||
class UpdateUserRequest(BaseModel):
|
||||
display_name: Optional[str] = None
|
||||
vaults: Optional[List[str]] = None
|
||||
active: Optional[bool] = None
|
||||
password: Optional[str] = None
|
||||
role: Optional[str] = None
|
||||
display_name: str | None = None
|
||||
vaults: list[str] | None = None
|
||||
active: bool | None = None
|
||||
password: str | None = None
|
||||
role: str | None = None
|
||||
|
||||
@validator("password")
|
||||
def password_valid(cls, v):
|
||||
if v is None:
|
||||
return v
|
||||
return validate_password_strength(v)
|
||||
|
||||
|
||||
# ── Public endpoints ──────────────────────────────────────────────────
|
||||
@@ -107,32 +139,54 @@ async def login(body: LoginRequest, response: Response, request: Request):
|
||||
raise HTTPException(403, "Compte désactivé")
|
||||
|
||||
# IP-based rate limiting (10 failures / 15 min per IP)
|
||||
client_ip = request.client.host if request.client else "unknown"
|
||||
client_ip = get_client_ip(request)
|
||||
if is_rate_limited(client_ip):
|
||||
raise HTTPException(429, "Trop de tentatives depuis cette adresse IP (15min)")
|
||||
|
||||
# BUG-031: per-account budget still applies when the attacker rotates IPs.
|
||||
if is_account_rate_limited(body.username):
|
||||
raise HTTPException(429, "Trop de tentatives sur ce compte (15min)")
|
||||
|
||||
if is_locked(body.username):
|
||||
raise HTTPException(429, "Compte temporairement verrouillé (15min)")
|
||||
|
||||
if not verify_password(body.password, user["password_hash"]):
|
||||
attempts = record_login_failure(body.username)
|
||||
rl_attempts, rl_remaining = rl_record_failure(client_ip)
|
||||
rl_record_account_failure(body.username)
|
||||
remaining = max(0, 5 - attempts)
|
||||
detail = "Identifiants invalides"
|
||||
if 0 < remaining <= 2:
|
||||
detail += f" ({remaining} tentative(s) restante(s))"
|
||||
raise HTTPException(401, detail)
|
||||
|
||||
# Success — clear rate limits and generate tokens
|
||||
record_login_success(body.username)
|
||||
# Success — clear rate limits
|
||||
rl_record_success(client_ip)
|
||||
|
||||
# If MFA is enabled, don't issue token yet — require second factor
|
||||
if user.get("mfa_enabled"):
|
||||
method = "totp" if user.get("mfa_secret") else _preferred_mfa_method(user)
|
||||
logger.info(f"User '{body.username}' login deferred — MFA required ({method})")
|
||||
return {
|
||||
"mfa_required": True,
|
||||
"mfa_method": method,
|
||||
"username": body.username,
|
||||
"remember_me": body.remember_me,
|
||||
}
|
||||
|
||||
return _issue_tokens(user, body.username, body.remember_me, response)
|
||||
|
||||
|
||||
def _issue_tokens(user: dict, username: str, remember_me: bool, response: Response) -> dict:
|
||||
"""Issue JWT tokens after successful authentication (password or MFA verified)."""
|
||||
record_login_success(username)
|
||||
rl_record_account_success(username)
|
||||
|
||||
access_token = create_access_token(user)
|
||||
refresh_token, refresh_jti = create_refresh_token(body.username)
|
||||
refresh_token, refresh_jti = create_refresh_token(username, remember=remember_me)
|
||||
|
||||
# Set refresh token as HttpOnly cookie (path-restricted to /api/auth/refresh)
|
||||
max_age = 2592000 if body.remember_me else 604800 # 30d or 7d
|
||||
import os
|
||||
max_age = 2592000 if remember_me else 604800 # 30d or 7d
|
||||
secure = os.environ.get("OBSIGATE_SECURE_COOKIES", "false").lower() == "true"
|
||||
response.set_cookie(
|
||||
key="refresh_token",
|
||||
@@ -143,10 +197,7 @@ async def login(body: LoginRequest, response: Response, request: Request):
|
||||
secure=secure,
|
||||
path="/api/auth/refresh",
|
||||
)
|
||||
|
||||
logger.info(f"User '{body.username}' logged in")
|
||||
|
||||
# Set access token as cookie for same-origin requests (e.g. popout window)
|
||||
logger.info(f"User '{username}' logged in")
|
||||
response.set_cookie(
|
||||
key="access_token",
|
||||
value=access_token,
|
||||
@@ -156,7 +207,6 @@ async def login(body: LoginRequest, response: Response, request: Request):
|
||||
secure=secure,
|
||||
path="/",
|
||||
)
|
||||
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"token_type": "bearer", # nosec B105 — OAuth2 token_type, pas un mot de passe
|
||||
@@ -175,6 +225,8 @@ async def refresh_token_endpoint(request: Request, response: Response):
|
||||
"""Renew access token via refresh token cookie.
|
||||
|
||||
Called automatically by the frontend when the access token expires.
|
||||
The refresh token is rotated on every use (BUG-027) and rejected if it
|
||||
predates the user's last password change (BUG-028).
|
||||
"""
|
||||
refresh_tok = request.cookies.get("refresh_token")
|
||||
if not refresh_tok:
|
||||
@@ -191,11 +243,38 @@ async def refresh_token_endpoint(request: Request, response: Response):
|
||||
if not user or not user.get("active"):
|
||||
raise HTTPException(401, "Utilisateur introuvable ou inactif")
|
||||
|
||||
# BUG-028: reject refresh tokens issued before the last password change.
|
||||
pca = user.get("password_changed_at")
|
||||
iat = payload.get("iat")
|
||||
if pca is not None and iat is not None:
|
||||
try:
|
||||
stale = int(iat) < int(float(pca))
|
||||
except (TypeError, ValueError):
|
||||
stale = True
|
||||
if stale:
|
||||
raise HTTPException(401, "Session expirée, veuillez vous reconnecter")
|
||||
|
||||
import os
|
||||
|
||||
secure = os.environ.get("OBSIGATE_SECURE_COOKIES", "false").lower() == "true"
|
||||
remember_me = bool(payload.get("remember", False))
|
||||
|
||||
# BUG-027: rotate the refresh token — the old one is now single-use.
|
||||
revoke_token(payload["jti"])
|
||||
new_refresh_token, _new_jti = create_refresh_token(user["username"], remember=remember_me)
|
||||
max_age = 2592000 if remember_me else 604800
|
||||
response.set_cookie(
|
||||
key="refresh_token",
|
||||
value=new_refresh_token,
|
||||
max_age=max_age,
|
||||
httponly=True,
|
||||
samesite="strict",
|
||||
secure=secure,
|
||||
path="/api/auth/refresh",
|
||||
)
|
||||
|
||||
new_access_token = create_access_token(user)
|
||||
|
||||
# Update cookies
|
||||
import os
|
||||
secure = os.environ.get("OBSIGATE_SECURE_COOKIES", "false").lower() == "true"
|
||||
response.set_cookie(
|
||||
key="access_token",
|
||||
value=new_access_token,
|
||||
@@ -217,18 +296,36 @@ async def refresh_token_endpoint(request: Request, response: Response):
|
||||
async def logout(
|
||||
request: Request,
|
||||
response: Response,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Logout: revoke refresh token and delete cookie."""
|
||||
"""Logout: revoke refresh and access tokens, then delete cookies."""
|
||||
refresh_tok = request.cookies.get("refresh_token")
|
||||
if refresh_tok:
|
||||
payload = decode_token(refresh_tok)
|
||||
if payload:
|
||||
revoke_token(payload["jti"])
|
||||
try:
|
||||
revoke_token(payload["jti"])
|
||||
except Exception:
|
||||
pass # token already revoked
|
||||
|
||||
# BUG-027: revoke the access token too, otherwise it stays valid until expiry.
|
||||
access_tok = None
|
||||
auth_header = request.headers.get("authorization", "")
|
||||
if auth_header.lower().startswith("bearer "):
|
||||
access_tok = auth_header[7:].strip()
|
||||
if not access_tok:
|
||||
access_tok = request.cookies.get("access_token")
|
||||
if access_tok:
|
||||
access_payload = decode_token(access_tok)
|
||||
if access_payload and access_payload.get("type") == "access":
|
||||
try:
|
||||
revoke_token(access_payload["jti"])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
response.delete_cookie("refresh_token", path="/api/auth/refresh")
|
||||
logger.info(f"User '{current_user['username']}' logged out")
|
||||
return {"message": "Déconnecté avec succès"}
|
||||
response.delete_cookie("access_token", path="/")
|
||||
response.delete_cookie("access_token", path="/api") # just in case
|
||||
return {"message": "Deconnecte avec succes"}
|
||||
|
||||
|
||||
@router.get("/me")
|
||||
@@ -239,22 +336,478 @@ async def get_me(current_user=Depends(require_auth)):
|
||||
"display_name": current_user["display_name"],
|
||||
"role": current_user["role"],
|
||||
"vaults": current_user["vaults"],
|
||||
"language": current_user.get("language", "fr"),
|
||||
"last_login": current_user.get("last_login"),
|
||||
}
|
||||
|
||||
|
||||
class UpdateMeRequest(BaseModel):
|
||||
"""Fields the user can update on their own profile."""
|
||||
display_name: str | None = None
|
||||
language: str | None = None
|
||||
|
||||
|
||||
@router.patch("/me")
|
||||
async def patch_me(req: UpdateMeRequest, current_user=Depends(require_auth)):
|
||||
"""Update current user's profile fields (display_name, language)."""
|
||||
from .user_store import update_user
|
||||
updates = {}
|
||||
if req.display_name is not None:
|
||||
updates["display_name"] = req.display_name
|
||||
if req.language is not None:
|
||||
if req.language not in ("fr", "en"):
|
||||
raise HTTPException(400, "language must be 'fr' or 'en'")
|
||||
updates["language"] = req.language
|
||||
if not updates:
|
||||
raise HTTPException(400, "No fields to update")
|
||||
updated = update_user(current_user["username"], updates)
|
||||
return {
|
||||
"username": updated["username"],
|
||||
"display_name": updated["display_name"],
|
||||
"role": updated["role"],
|
||||
"vaults": updated["vaults"],
|
||||
"language": updated.get("language", "fr"),
|
||||
"last_login": updated.get("last_login"),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/change-password")
|
||||
async def change_password(
|
||||
req: ChangePasswordRequest,
|
||||
response: Response,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Change own password."""
|
||||
"""Change own password.
|
||||
|
||||
BUG-028: changing the password invalidates all previously issued tokens;
|
||||
a fresh pair is issued to keep the current session alive.
|
||||
"""
|
||||
user = get_user(current_user["username"])
|
||||
assert user is not None, f"User {current_user['username']} not found"
|
||||
if not verify_password(req.current_password, user["password_hash"]):
|
||||
raise HTTPException(400, "Mot de passe actuel incorrect")
|
||||
update_user(current_user["username"], {"password": req.new_password})
|
||||
return {"message": "Mot de passe mis à jour"}
|
||||
updated = get_user(current_user["username"])
|
||||
result: dict = {"message": "Mot de passe mis à jour"}
|
||||
if updated is not None:
|
||||
result.update(_issue_tokens(updated, updated["username"], False, response))
|
||||
return result
|
||||
|
||||
|
||||
# ── MFA endpoints ────────────────────────────────────────────────────
|
||||
|
||||
def _enforce_mfa_rate_limit(request: Request, username: str) -> str:
|
||||
"""Reject MFA attempts from a rate-limited IP or on a locked account.
|
||||
|
||||
BUG-023: the second-factor endpoints were previously unprotected, making
|
||||
the 6-digit TOTP brute-forceable. Returns the resolved client IP.
|
||||
"""
|
||||
client_ip = get_client_ip(request)
|
||||
if is_rate_limited(client_ip):
|
||||
raise HTTPException(429, "Trop de tentatives depuis cette adresse IP (15min)")
|
||||
if is_account_rate_limited(username):
|
||||
raise HTTPException(429, "Trop de tentatives sur ce compte (15min)")
|
||||
if is_locked(username):
|
||||
raise HTTPException(429, "Compte temporairement verrouillé (15min)")
|
||||
return client_ip
|
||||
|
||||
|
||||
def _record_mfa_failure(client_ip: str, username: str) -> None:
|
||||
"""Record a failed MFA attempt for the IP, the account and the lockout."""
|
||||
record_login_failure(username)
|
||||
rl_record_failure(client_ip)
|
||||
rl_record_account_failure(username)
|
||||
|
||||
|
||||
class MfaVerifyRequest(BaseModel):
|
||||
username: str
|
||||
code: str
|
||||
remember_me: bool = False
|
||||
|
||||
|
||||
class MfaRecoveryRequest(BaseModel):
|
||||
username: str
|
||||
recovery_code: str
|
||||
|
||||
|
||||
class MfaDisableRequest(BaseModel):
|
||||
password: str
|
||||
code: str
|
||||
|
||||
|
||||
class MfaEnableRequest(BaseModel):
|
||||
code: str
|
||||
|
||||
|
||||
@router.post("/mfa/totp/setup")
|
||||
async def mfa_totp_setup(current_user=Depends(require_auth)):
|
||||
"""Generate a TOTP secret and QR URI for MFA setup.
|
||||
|
||||
Returns the secret and otpauth URI — client displays QR code.
|
||||
Does NOT enable MFA yet; call /mfa/totp/enable after first successful verify.
|
||||
"""
|
||||
from .user_store import update_user
|
||||
secret = generate_secret()
|
||||
qr_uri = generate_qr_uri(secret, current_user["username"])
|
||||
# Store secret temporarily (not yet enabled)
|
||||
update_user(current_user["username"], {
|
||||
"mfa_secret_pending": secret,
|
||||
})
|
||||
return {
|
||||
"secret": secret,
|
||||
"qr_uri": qr_uri,
|
||||
"otpauth_uri": qr_uri,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/mfa/totp/enable")
|
||||
async def mfa_totp_enable(
|
||||
req: MfaEnableRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Enable MFA after verifying the first TOTP code.
|
||||
|
||||
On success: generates recovery codes, enables MFA, returns recovery codes.
|
||||
"""
|
||||
from .user_store import get_user, update_user
|
||||
|
||||
user = get_user(current_user["username"])
|
||||
if user is None:
|
||||
raise HTTPException(404, "Utilisateur introuvable")
|
||||
secret = user.get("mfa_secret_pending")
|
||||
if not secret:
|
||||
raise HTTPException(400, "Aucune configuration MFA en cours. Commencez par /mfa/totp/setup")
|
||||
|
||||
if not verify_totp(secret, req.code):
|
||||
raise HTTPException(400, "Code TOTP invalide")
|
||||
|
||||
# Generate recovery codes
|
||||
recovery_codes = generate_recovery_codes()
|
||||
hashed_codes = [hash_recovery_code(c) for c in recovery_codes]
|
||||
|
||||
# Enable MFA
|
||||
update_user(current_user["username"], {
|
||||
"mfa_enabled": True,
|
||||
"mfa_secret": secret,
|
||||
"mfa_method": "totp",
|
||||
"mfa_recovery_codes": hashed_codes,
|
||||
"mfa_secret_pending": None, # clear pending
|
||||
})
|
||||
|
||||
logger.info(f"MFA enabled for user '{current_user['username']}'")
|
||||
return {
|
||||
"mfa_enabled": True,
|
||||
"recovery_codes": recovery_codes, # shown once, client must display/save
|
||||
}
|
||||
|
||||
|
||||
@router.post("/mfa/totp/disable")
|
||||
async def mfa_totp_disable(
|
||||
req: MfaDisableRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Disable MFA. Requires current password + valid TOTP code."""
|
||||
from .user_store import get_user, update_user
|
||||
|
||||
user = get_user(current_user["username"])
|
||||
if user is None:
|
||||
raise HTTPException(404, "Utilisateur introuvable")
|
||||
if not user.get("mfa_enabled"):
|
||||
raise HTTPException(400, "MFA non activé")
|
||||
|
||||
if not verify_password(req.password, user["password_hash"]):
|
||||
raise HTTPException(400, "Mot de passe incorrect")
|
||||
|
||||
if not verify_totp(user["mfa_secret"], req.code):
|
||||
raise HTTPException(400, "Code TOTP invalide")
|
||||
|
||||
update_user(current_user["username"], {
|
||||
"mfa_enabled": False,
|
||||
"mfa_secret": None,
|
||||
"mfa_method": None,
|
||||
"mfa_recovery_codes": [],
|
||||
})
|
||||
|
||||
logger.info(f"MFA disabled for user '{current_user['username']}'")
|
||||
return {"mfa_enabled": False}
|
||||
|
||||
|
||||
# ── WebAuthn endpoints (ROADMAP #64) ─────────────────────────────────
|
||||
|
||||
def _preferred_mfa_method(user: dict) -> str:
|
||||
"""Which second factor to offer at login: webauthn when keys exist, else totp."""
|
||||
if user.get("webauthn_credentials"):
|
||||
return "webauthn"
|
||||
return "totp"
|
||||
|
||||
|
||||
class WebauthnRegisterRequest(BaseModel):
|
||||
credential: dict
|
||||
label: str = ""
|
||||
|
||||
|
||||
class WebauthnVerifyRequest(BaseModel):
|
||||
username: str
|
||||
credential: dict
|
||||
remember_me: bool = False
|
||||
|
||||
|
||||
class WebauthnRemoveRequest(BaseModel):
|
||||
credential_id: str
|
||||
password: str
|
||||
|
||||
|
||||
@router.post("/mfa/webauthn/register/options")
|
||||
async def mfa_webauthn_register_options(current_user=Depends(require_auth)):
|
||||
"""Start WebAuthn key enrolment — returns publicKey creation options for the browser."""
|
||||
from .webauthn_mfa import begin_registration
|
||||
|
||||
options = begin_registration(current_user["username"],
|
||||
current_user.get("display_name", ""))
|
||||
return {"options": options}
|
||||
|
||||
|
||||
@router.post("/mfa/webauthn/register")
|
||||
async def mfa_webauthn_register(
|
||||
req: WebauthnRegisterRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Verify the created credential, store it, and enable MFA if not already on.
|
||||
|
||||
Returns recovery codes when MFA is newly enabled (they were never issued).
|
||||
"""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from .user_store import get_user, update_user
|
||||
from .webauthn_mfa import complete_registration
|
||||
|
||||
user = get_user(current_user["username"])
|
||||
if user is None:
|
||||
raise HTTPException(404, "Utilisateur introuvable")
|
||||
try:
|
||||
record = complete_registration(current_user["username"], req.credential,
|
||||
label=req.label)
|
||||
except ValueError as e:
|
||||
raise HTTPException(400, str(e))
|
||||
except Exception as e:
|
||||
logger.warning(f"WebAuthn registration failed for {current_user['username']}: {e}")
|
||||
raise HTTPException(400, "Validation du credential WebAuthn échouée")
|
||||
|
||||
record["registered_at"] = datetime.now(timezone.utc).isoformat()
|
||||
creds = list(user.get("webauthn_credentials", []))
|
||||
creds = [c for c in creds if c.get("credential_id") != record["credential_id"]]
|
||||
creds.append(record)
|
||||
|
||||
updates: dict = {"webauthn_credentials": creds}
|
||||
issued_recovery: list[str] = []
|
||||
if not user.get("mfa_enabled"):
|
||||
issued_recovery = generate_recovery_codes()
|
||||
updates.update({
|
||||
"mfa_enabled": True,
|
||||
"mfa_method": "webauthn",
|
||||
"mfa_recovery_codes": [hash_recovery_code(c) for c in issued_recovery],
|
||||
})
|
||||
update_user(current_user["username"], updates)
|
||||
|
||||
logger.info(f"WebAuthn credential registered for user '{current_user['username']}' "
|
||||
f"({record['label']})")
|
||||
return {
|
||||
"ok": True,
|
||||
"credentials": user_credentials_response(creds),
|
||||
"mfa_enabled": True,
|
||||
"recovery_codes": issued_recovery,
|
||||
}
|
||||
|
||||
|
||||
def user_credentials_response(creds: list[dict]) -> list[dict]:
|
||||
from .webauthn_mfa import credentials_for_api
|
||||
return credentials_for_api(creds)
|
||||
|
||||
|
||||
@router.get("/mfa/webauthn/credentials")
|
||||
async def mfa_webauthn_list(current_user=Depends(require_auth)):
|
||||
from .user_store import get_user
|
||||
user = get_user(current_user["username"])
|
||||
if user is None:
|
||||
raise HTTPException(404, "Utilisateur introuvable")
|
||||
return {"credentials": user_credentials_response(user.get("webauthn_credentials", []))}
|
||||
|
||||
|
||||
@router.post("/mfa/webauthn/credentials/remove")
|
||||
async def mfa_webauthn_remove(
|
||||
req: WebauthnRemoveRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Remove a WebAuthn key. Requires password. Disables MFA if no second factor remains."""
|
||||
from .user_store import get_user, update_user
|
||||
from .webauthn_mfa import clear_pending
|
||||
|
||||
user = get_user(current_user["username"])
|
||||
if user is None:
|
||||
raise HTTPException(404, "Utilisateur introuvable")
|
||||
if not verify_password(req.password, user["password_hash"]):
|
||||
raise HTTPException(400, "Mot de passe incorrect")
|
||||
|
||||
creds = [c for c in user.get("webauthn_credentials", [])
|
||||
if c.get("credential_id") != req.credential_id]
|
||||
if len(creds) == len(user.get("webauthn_credentials", [])):
|
||||
raise HTTPException(404, "Credential inconnu")
|
||||
|
||||
updates: dict = {"webauthn_credentials": creds}
|
||||
if not creds and not user.get("mfa_secret"):
|
||||
updates.update({"mfa_enabled": False, "mfa_method": None, "mfa_recovery_codes": []})
|
||||
elif not creds and user.get("mfa_secret"):
|
||||
updates["mfa_method"] = "totp"
|
||||
update_user(current_user["username"], updates)
|
||||
clear_pending(current_user["username"])
|
||||
return {"ok": True, "credentials": user_credentials_response(creds),
|
||||
"mfa_enabled": bool(updates.get("mfa_enabled", user.get("mfa_enabled"))) and bool(creds or user.get("mfa_secret"))}
|
||||
|
||||
|
||||
@router.post("/mfa/webauthn/options")
|
||||
async def mfa_webauthn_login_options(body: dict = Body(...)):
|
||||
"""Unauthenticated: begin the login assertion for a user with registered keys.
|
||||
|
||||
Enumeration-safe: always 200 — returns null options (caller falls back to
|
||||
TOTP/recovery UI) when the user has no WebAuthn key or MFA is off.
|
||||
"""
|
||||
username = str(body.get("username", ""))
|
||||
user = get_user(username)
|
||||
creds = (user or {}).get("webauthn_credentials", [])
|
||||
if not user or not user.get("mfa_enabled") or not creds:
|
||||
return {"mfa_method": "totp", "options": None}
|
||||
|
||||
from .webauthn_mfa import begin_authentication
|
||||
options = begin_authentication(username, creds)
|
||||
if options is None:
|
||||
return {"mfa_method": "totp", "options": None}
|
||||
return {"mfa_method": "webauthn", "options": options}
|
||||
|
||||
|
||||
@router.post("/mfa/webauthn/verify")
|
||||
async def mfa_webauthn_verify(
|
||||
body: WebauthnVerifyRequest,
|
||||
response: Response,
|
||||
request: Request,
|
||||
):
|
||||
"""Unauthenticated: verify the WebAuthn assertion and issue JWT tokens."""
|
||||
from .user_store import get_user, update_user
|
||||
from .webauthn_mfa import complete_authentication
|
||||
|
||||
client_ip = _enforce_mfa_rate_limit(request, body.username)
|
||||
|
||||
user = get_user(body.username)
|
||||
if not user:
|
||||
hash_password("dummy_timing_protection")
|
||||
raise HTTPException(401, "Identifiants invalides")
|
||||
if not user.get("mfa_enabled"):
|
||||
raise HTTPException(400, "MFA non activé pour cet utilisateur")
|
||||
|
||||
creds = user.get("webauthn_credentials", [])
|
||||
try:
|
||||
credential_id = body.credential.get("id", "")
|
||||
stored = next((c for c in creds if c.get("credential_id") == credential_id), None)
|
||||
if stored is None:
|
||||
raise ValueError("Credential non enregistré")
|
||||
new_count = complete_authentication(body.username, body.credential, stored)
|
||||
except ValueError as e:
|
||||
_record_mfa_failure(client_ip, body.username)
|
||||
raise HTTPException(401, str(e))
|
||||
except Exception as e:
|
||||
_record_mfa_failure(client_ip, body.username)
|
||||
logger.warning(f"WebAuthn verification failed for {body.username}: {e}")
|
||||
raise HTTPException(401, "Vérification WebAuthn échouée")
|
||||
|
||||
updated = [dict(c) for c in creds]
|
||||
for c in updated:
|
||||
if c.get("credential_id") == body.credential.get("id"):
|
||||
c["sign_count"] = new_count
|
||||
update_user(body.username, {"webauthn_credentials": updated})
|
||||
|
||||
rl_record_success(client_ip)
|
||||
logger.info(f"User '{body.username}' logged in via WebAuthn")
|
||||
return _issue_tokens(user, body.username, body.remember_me, response)
|
||||
|
||||
|
||||
@router.get("/mfa/status")
|
||||
async def mfa_status(current_user=Depends(require_auth)):
|
||||
"""Return current user's MFA status."""
|
||||
from .user_store import get_user
|
||||
user = get_user(current_user["username"])
|
||||
return {
|
||||
"mfa_enabled": user.get("mfa_enabled", False),
|
||||
"mfa_method": user.get("mfa_method"),
|
||||
"totp_enabled": bool(user.get("mfa_secret")),
|
||||
"webauthn_credentials": len(user.get("webauthn_credentials", [])),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/mfa/totp/verify")
|
||||
async def mfa_totp_verify(body: MfaVerifyRequest, response: Response, request: Request):
|
||||
"""Verify TOTP code during login (second factor).
|
||||
|
||||
Called after login returns mfa_required=true.
|
||||
On success: issues JWT tokens.
|
||||
"""
|
||||
from .user_store import get_user
|
||||
|
||||
client_ip = _enforce_mfa_rate_limit(request, body.username)
|
||||
|
||||
user = get_user(body.username)
|
||||
if not user:
|
||||
# Timing-safe: simulate work
|
||||
hash_password("dummy_timing_protection")
|
||||
raise HTTPException(401, "Identifiants invalides")
|
||||
|
||||
if not user.get("mfa_enabled") or not user.get("mfa_secret"):
|
||||
raise HTTPException(400, "MFA non activé pour cet utilisateur")
|
||||
|
||||
if not verify_totp(user["mfa_secret"], body.code):
|
||||
_record_mfa_failure(client_ip, body.username)
|
||||
raise HTTPException(401, "Code TOTP invalide")
|
||||
|
||||
# Clear IP rate limit on success
|
||||
rl_record_success(client_ip)
|
||||
|
||||
return _issue_tokens(user, body.username, body.remember_me, response)
|
||||
|
||||
|
||||
@router.post("/mfa/recovery")
|
||||
async def mfa_recovery_login(body: MfaRecoveryRequest, response: Response, request: Request):
|
||||
"""Login with a recovery code (when TOTP device is unavailable).
|
||||
|
||||
Each recovery code is single-use.
|
||||
"""
|
||||
from .user_store import get_user, update_user
|
||||
|
||||
client_ip = _enforce_mfa_rate_limit(request, body.username)
|
||||
|
||||
user = get_user(body.username)
|
||||
if not user:
|
||||
hash_password("dummy_timing_protection")
|
||||
raise HTTPException(401, "Identifiants invalides")
|
||||
|
||||
if not user.get("mfa_enabled"):
|
||||
raise HTTPException(400, "MFA non activé pour cet utilisateur")
|
||||
|
||||
hashed_codes = user.get("mfa_recovery_codes", [])
|
||||
if not hashed_codes:
|
||||
raise HTTPException(400, "Aucun code de récupération disponible")
|
||||
|
||||
idx = verify_recovery_code(body.recovery_code, hashed_codes)
|
||||
if idx is None:
|
||||
_record_mfa_failure(client_ip, body.username)
|
||||
raise HTTPException(401, "Code de récupération invalide")
|
||||
|
||||
# Remove used recovery code (single-use)
|
||||
hashed_codes.pop(idx)
|
||||
update_user(body.username, {"mfa_recovery_codes": hashed_codes})
|
||||
|
||||
# Clear IP rate limit
|
||||
rl_record_success(client_ip)
|
||||
|
||||
logger.info(f"User '{body.username}' logged in via recovery code")
|
||||
return _issue_tokens(user, body.username, False, response)
|
||||
|
||||
|
||||
# ── Admin endpoints ───────────────────────────────────────────────────
|
||||
|
||||
+70
-55
@@ -4,12 +4,12 @@
|
||||
# via tmp+rename to prevent corruption on crash.
|
||||
|
||||
import json
|
||||
import uuid
|
||||
import logging
|
||||
import shutil
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional, List
|
||||
|
||||
from .password import hash_password
|
||||
|
||||
@@ -17,6 +17,12 @@ logger = logging.getLogger("obsigate.auth.users")
|
||||
|
||||
USERS_FILE = Path("data/users.json")
|
||||
|
||||
# Serialises read-modify-write cycles on users.json. ``RLock`` because a few
|
||||
# helpers (e.g. ``record_login_failure``) call other mutators while holding it.
|
||||
# BUG-029: without this, concurrent MFA enable + password change could lose one
|
||||
# of the two updates (last writer wins).
|
||||
_users_lock = threading.RLock()
|
||||
|
||||
|
||||
def _read() -> dict:
|
||||
"""Read users.json. Returns empty structure if file doesn't exist."""
|
||||
@@ -54,12 +60,12 @@ def has_users() -> bool:
|
||||
return bool(_read()["users"])
|
||||
|
||||
|
||||
def get_user(username: str) -> Optional[dict]:
|
||||
def get_user(username: str) -> dict | None:
|
||||
"""Get a user by username. Returns None if not found."""
|
||||
return _read()["users"].get(username)
|
||||
|
||||
|
||||
def get_all_users() -> List[dict]:
|
||||
def get_all_users() -> list[dict]:
|
||||
"""Get all users WITHOUT password_hash (safe for API responses)."""
|
||||
users = _read()["users"]
|
||||
return [
|
||||
@@ -72,29 +78,32 @@ def create_user(
|
||||
username: str,
|
||||
password: str,
|
||||
role: str = "user",
|
||||
vaults: Optional[List[str]] = None,
|
||||
display_name: Optional[str] = None,
|
||||
vaults: list[str] | None = None,
|
||||
display_name: str | None = None,
|
||||
) -> dict:
|
||||
"""Create a new user. Raises ValueError if username already taken."""
|
||||
data = _read()
|
||||
if username in data["users"]:
|
||||
raise ValueError(f"User '{username}' already exists")
|
||||
with _users_lock:
|
||||
data = _read()
|
||||
if username in data["users"]:
|
||||
raise ValueError(f"User '{username}' already exists")
|
||||
|
||||
user = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"username": username,
|
||||
"display_name": display_name or username,
|
||||
"password_hash": hash_password(password),
|
||||
"role": role,
|
||||
"vaults": vaults or [],
|
||||
"active": True,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"last_login": None,
|
||||
"failed_attempts": 0,
|
||||
"locked_until": None,
|
||||
}
|
||||
data["users"][username] = user
|
||||
_write(data)
|
||||
user = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"username": username,
|
||||
"display_name": display_name or username,
|
||||
"password_hash": hash_password(password),
|
||||
"role": role,
|
||||
"vaults": vaults or [],
|
||||
"active": True,
|
||||
"language": "fr", # default UI language
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"password_changed_at": datetime.now(timezone.utc).timestamp(),
|
||||
"last_login": None,
|
||||
"failed_attempts": 0,
|
||||
"locked_until": None,
|
||||
}
|
||||
data["users"][username] = user
|
||||
_write(data)
|
||||
logger.info(f"Created user '{username}' (role={role})")
|
||||
return {k: v for k, v in user.items() if k != "password_hash"}
|
||||
|
||||
@@ -105,28 +114,33 @@ def update_user(username: str, updates: dict) -> dict:
|
||||
Forbidden fields (id, username, created_at) are silently ignored.
|
||||
If 'password' is in updates, it's hashed and stored as password_hash.
|
||||
"""
|
||||
data = _read()
|
||||
if username not in data["users"]:
|
||||
raise ValueError(f"User '{username}' not found")
|
||||
with _users_lock:
|
||||
data = _read()
|
||||
if username not in data["users"]:
|
||||
raise ValueError(f"User '{username}' not found")
|
||||
|
||||
forbidden = {"id", "username", "created_at"}
|
||||
safe_updates = {k: v for k, v in updates.items() if k not in forbidden}
|
||||
forbidden = {"id", "username", "created_at"}
|
||||
safe_updates = {k: v for k, v in updates.items() if k not in forbidden}
|
||||
|
||||
if "password" in safe_updates:
|
||||
safe_updates["password_hash"] = hash_password(safe_updates.pop("password"))
|
||||
if "password" in safe_updates:
|
||||
safe_updates["password_hash"] = hash_password(safe_updates.pop("password"))
|
||||
# BUG-028: invalidate every token issued before this change.
|
||||
safe_updates["password_changed_at"] = datetime.now(timezone.utc).timestamp()
|
||||
|
||||
data["users"][username].update(safe_updates)
|
||||
_write(data)
|
||||
return {k: v for k, v in data["users"][username].items() if k != "password_hash"}
|
||||
data["users"][username].update(safe_updates)
|
||||
_write(data)
|
||||
result = {k: v for k, v in data["users"][username].items() if k != "password_hash"}
|
||||
return result
|
||||
|
||||
|
||||
def delete_user(username: str):
|
||||
"""Delete a user. Raises ValueError if not found."""
|
||||
data = _read()
|
||||
if username not in data["users"]:
|
||||
raise ValueError(f"User '{username}' not found")
|
||||
del data["users"][username]
|
||||
_write(data)
|
||||
with _users_lock:
|
||||
data = _read()
|
||||
if username not in data["users"]:
|
||||
raise ValueError(f"User '{username}' not found")
|
||||
del data["users"][username]
|
||||
_write(data)
|
||||
logger.info(f"Deleted user '{username}'")
|
||||
|
||||
|
||||
@@ -144,24 +158,25 @@ def record_login_failure(username: str) -> int:
|
||||
|
||||
After 5 failures, locks the account for 15 minutes.
|
||||
"""
|
||||
data = _read()
|
||||
user = data["users"].get(username)
|
||||
if not user:
|
||||
return 0
|
||||
with _users_lock:
|
||||
data = _read()
|
||||
user = data["users"].get(username)
|
||||
if not user:
|
||||
return 0
|
||||
|
||||
attempts = user.get("failed_attempts", 0) + 1
|
||||
updates = {"failed_attempts": attempts}
|
||||
attempts = user.get("failed_attempts", 0) + 1
|
||||
updates = {"failed_attempts": attempts}
|
||||
|
||||
# Lock after 5 failed attempts (15 minutes)
|
||||
if attempts >= 5:
|
||||
locked_until = (
|
||||
datetime.now(timezone.utc) + timedelta(minutes=15)
|
||||
).isoformat()
|
||||
updates["locked_until"] = locked_until
|
||||
logger.warning(f"Account '{username}' locked after {attempts} failed attempts")
|
||||
# Lock after 5 failed attempts (15 minutes)
|
||||
if attempts >= 5:
|
||||
locked_until = (
|
||||
datetime.now(timezone.utc) + timedelta(minutes=15)
|
||||
).isoformat()
|
||||
updates["locked_until"] = locked_until
|
||||
logger.warning(f"Account '{username}' locked after {attempts} failed attempts")
|
||||
|
||||
update_user(username, updates)
|
||||
return attempts
|
||||
update_user(username, updates)
|
||||
return attempts
|
||||
|
||||
|
||||
def is_locked(username: str) -> bool:
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
# backend/auth/webauthn_mfa.py
|
||||
# WebAuthn support for MFA (ROADMAP #64): security keys / platform biometrics.
|
||||
# Thin wrapper over the `webauthn` library with an in-memory challenge store.
|
||||
#
|
||||
# Credentials are persisted in users.json under "webauthn_credentials":
|
||||
# [{ "credential_id": <b64url>, "public_key": <b64url>, "sign_count": int,
|
||||
# "transports": [...], "label": str, "registered_at": iso }]
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from webauthn import (
|
||||
generate_authentication_options,
|
||||
generate_registration_options,
|
||||
options_to_json,
|
||||
verify_authentication_response,
|
||||
verify_registration_response,
|
||||
)
|
||||
from webauthn.helpers import (
|
||||
base64url_to_bytes,
|
||||
bytes_to_base64url,
|
||||
parse_authentication_credential_json,
|
||||
parse_registration_credential_json,
|
||||
)
|
||||
from webauthn.helpers.structs import (
|
||||
AuthenticatorSelectionCriteria,
|
||||
ResidentKeyRequirement,
|
||||
UserVerificationRequirement,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("obsigate.auth.webauthn")
|
||||
|
||||
# Challenge lifetime: clients have 3 minutes to complete the ceremony.
|
||||
CHALLENGE_TTL_SECONDS = 180
|
||||
|
||||
# In-memory pending challenges: key -> (challenge_bytes, expires_at)
|
||||
_pending: dict[str, tuple[bytes, float]] = {}
|
||||
|
||||
|
||||
def rp_id() -> str:
|
||||
return os.environ.get("OBSIGATE_WEBAUTHN_RP_ID", "localhost")
|
||||
|
||||
|
||||
def rp_name() -> str:
|
||||
return os.environ.get("OBSIGATE_WEBAUTHN_RP_NAME", "ObsiGate")
|
||||
|
||||
|
||||
def expected_origins() -> list[str]:
|
||||
raw = os.environ.get("OBSIGATE_WEBAUTHN_ORIGINS", "http://localhost")
|
||||
return [o.strip() for o in raw.split(",") if o.strip()]
|
||||
|
||||
|
||||
def _prune_expired() -> None:
|
||||
now = time.time()
|
||||
for key in [k for k, (_, exp) in _pending.items() if exp < now]:
|
||||
_pending.pop(key, None)
|
||||
|
||||
|
||||
def _store_challenge(key: str) -> bytes:
|
||||
_prune_expired()
|
||||
challenge = secrets.token_bytes(32)
|
||||
_pending[key] = (challenge, time.time() + CHALLENGE_TTL_SECONDS)
|
||||
return challenge
|
||||
|
||||
|
||||
def _take_challenge(key: str) -> bytes | None:
|
||||
"""Pop a challenge (single-use). Returns None if missing/expired."""
|
||||
_prune_expired()
|
||||
entry = _pending.pop(key, None)
|
||||
return entry[0] if entry else None
|
||||
|
||||
|
||||
def clear_pending(username: str) -> None:
|
||||
"""Drop all pending challenges for a user (e.g. after enable/disable)."""
|
||||
for key in [k for k in _pending if k.startswith(f"{username}:")]:
|
||||
_pending.pop(key, None)
|
||||
|
||||
|
||||
# ── Registration (enrol a key in settings) ─────────────────────────────
|
||||
|
||||
def begin_registration(username: str, display_name: str) -> dict:
|
||||
options = generate_registration_options(
|
||||
rp_id=rp_id(),
|
||||
rp_name=rp_name(),
|
||||
user_name=username,
|
||||
user_display_name=display_name or username,
|
||||
challenge=_store_challenge(f"{username}:register"),
|
||||
authenticator_selection=AuthenticatorSelectionCriteria(
|
||||
resident_key=ResidentKeyRequirement.PREFERRED,
|
||||
user_verification=UserVerificationRequirement.PREFERRED,
|
||||
),
|
||||
)
|
||||
return _finalize_options(options)
|
||||
|
||||
|
||||
def complete_registration(username: str, credential_json: dict[str, Any],
|
||||
label: str = "") -> dict:
|
||||
challenge = _take_challenge(f"{username}:register")
|
||||
if challenge is None:
|
||||
raise ValueError("Session d'enregistrement expirée — recommencez")
|
||||
|
||||
credential = parse_registration_credential_json(credential_json)
|
||||
verification = verify_registration_response(
|
||||
credential=credential,
|
||||
expected_challenge=challenge,
|
||||
expected_rp_id=rp_id(),
|
||||
expected_origin=expected_origins(),
|
||||
)
|
||||
|
||||
transports = credential.response.transports or []
|
||||
label = (label or str(credential_json.get("label") or "")).strip() or "Security key"
|
||||
record = {
|
||||
"credential_id": bytes_to_base64url(verification.credential_id),
|
||||
"public_key": bytes_to_base64url(verification.credential_public_key),
|
||||
"sign_count": int(verification.sign_count),
|
||||
"transports": [str(t) for t in transports],
|
||||
"label": label[:60],
|
||||
}
|
||||
return record
|
||||
|
||||
|
||||
# ── Authentication (assertion at login) ────────────────────────────────
|
||||
|
||||
def begin_authentication(username: str, credentials: list[dict]) -> dict | None:
|
||||
if not credentials:
|
||||
return None
|
||||
from webauthn.helpers.structs import PublicKeyCredentialDescriptor
|
||||
|
||||
allow = [
|
||||
PublicKeyCredentialDescriptor(id=base64url_to_bytes(c["credential_id"]))
|
||||
for c in credentials
|
||||
]
|
||||
options = generate_authentication_options(
|
||||
rp_id=rp_id(),
|
||||
challenge=_store_challenge(f"{username}:login"),
|
||||
allow_credentials=allow,
|
||||
)
|
||||
return _finalize_options(options)
|
||||
|
||||
|
||||
def complete_authentication(
|
||||
username: str,
|
||||
credential_json: dict[str, Any],
|
||||
stored: dict,
|
||||
) -> int:
|
||||
"""Verify an assertion. Returns the new sign_count. Raises ValueError on failure."""
|
||||
challenge = _take_challenge(f"{username}:login")
|
||||
if challenge is None:
|
||||
raise ValueError("Session expirée — rechargez la page")
|
||||
|
||||
credential = parse_authentication_credential_json(credential_json)
|
||||
verification = verify_authentication_response(
|
||||
credential=credential,
|
||||
expected_challenge=challenge,
|
||||
expected_rp_id=rp_id(),
|
||||
expected_origin=expected_origins(),
|
||||
credential_public_key=base64url_to_bytes(stored["public_key"]),
|
||||
credential_current_sign_count=int(stored.get("sign_count", 0)),
|
||||
)
|
||||
return int(verification.new_sign_count)
|
||||
|
||||
|
||||
def credentials_for_api(credentials: list[dict]) -> list[dict]:
|
||||
"""Sanitized view for the settings UI (no public keys)."""
|
||||
return [
|
||||
{
|
||||
"credential_id": c.get("credential_id"),
|
||||
"label": c.get("label", "Security key"),
|
||||
"transports": c.get("transports", []),
|
||||
"registered_at": c.get("registered_at"),
|
||||
}
|
||||
for c in credentials
|
||||
]
|
||||
|
||||
|
||||
def _finalize_options(options) -> dict:
|
||||
import json
|
||||
|
||||
return json.loads(options_to_json(options))
|
||||
@@ -0,0 +1,624 @@
|
||||
"""BooksLM — Context collection and caching for directory-scoped AI chat.
|
||||
|
||||
Collects markdown files from an Obsidian vault directory, applies secret
|
||||
redaction, builds a system prompt with file contents, and caches results
|
||||
for repeated queries.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import mimetypes
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from backend.secret_redactor import redact_file_content
|
||||
|
||||
logger = logging.getLogger("obsigate.bookslm")
|
||||
|
||||
# ── Configuration limits ──
|
||||
BOOKSLM_MAX_FILES = int(os.getenv("BOOKSLM_MAX_FILES", "200"))
|
||||
BOOKSLM_MAX_TOTAL_CHARS = int(os.getenv("BOOKSLM_MAX_TOTAL_CHARS", "200000"))
|
||||
BOOKSLM_MAX_FILE_CHARS = int(os.getenv("BOOKSLM_MAX_FILE_CHARS", "30000"))
|
||||
# Maximum size of an image sent to a vision model (bytes, before base64).
|
||||
BOOKSLM_MAX_IMAGE_BYTES = int(os.getenv("BOOKSLM_MAX_IMAGE_BYTES", "10000000"))
|
||||
|
||||
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".avif"}
|
||||
|
||||
# ── Cache ──
|
||||
_cache: dict[str, dict[str, Any]] = {}
|
||||
_CACHE_TTL = 300 # seconds
|
||||
|
||||
|
||||
def _cache_key(vault_path: Path, directory: str, file_mtimes: list[tuple[str, float]]) -> str:
|
||||
"""Build a SHA-256 cache key from vault+directory+file modification times."""
|
||||
raw = json.dumps({
|
||||
"vault": str(vault_path),
|
||||
"dir": directory,
|
||||
"mtimes": sorted(file_mtimes),
|
||||
}, sort_keys=True)
|
||||
return hashlib.sha256(raw.encode()).hexdigest()
|
||||
|
||||
|
||||
def _should_skip(name: str) -> bool:
|
||||
"""Return True if this file/directory name should be skipped."""
|
||||
skip_prefixes = (".",)
|
||||
skip_names = {"_attachments", "node_modules", ".git", ".obsidian", "__pycache__"}
|
||||
if name in skip_names:
|
||||
return True
|
||||
return bool(any(name.startswith(p) for p in skip_prefixes))
|
||||
|
||||
|
||||
def _file_priority(path: Path) -> tuple[int, float]:
|
||||
"""Sort key: README/index first, then by modification time descending.
|
||||
|
||||
Returns (priority_group, -mtime) so that:
|
||||
- Group 0: README* and index* files (come first)
|
||||
- Group 1: all other files (come after)
|
||||
Within each group, newer files come first.
|
||||
"""
|
||||
name_lower = path.stem.lower()
|
||||
if name_lower.startswith("readme") or name_lower.startswith("index"):
|
||||
group = 0
|
||||
else:
|
||||
group = 1
|
||||
try:
|
||||
mtime = path.stat().st_mtime
|
||||
except OSError:
|
||||
mtime = 0.0
|
||||
return (group, -mtime)
|
||||
|
||||
|
||||
def collect_directory_context(vault_path: Path, directory: str) -> dict[str, Any]:
|
||||
"""Walk a directory recursively, collect .md files with content.
|
||||
|
||||
Args:
|
||||
vault_path: Absolute path to the vault root.
|
||||
directory: Relative directory path within the vault (empty = root).
|
||||
|
||||
Returns:
|
||||
Dict with keys: files, total_chars, file_count, directory_tree.
|
||||
"""
|
||||
target_dir = (vault_path / directory).resolve() if directory else vault_path.resolve()
|
||||
vault_resolved = vault_path.resolve()
|
||||
|
||||
# Safety: ensure target is within vault
|
||||
try:
|
||||
target_dir.relative_to(vault_resolved)
|
||||
except ValueError:
|
||||
logger.warning(f"Directory outside vault: {target_dir}")
|
||||
return {"files": [], "total_chars": 0, "file_count": 0, "directory_tree": ""}
|
||||
|
||||
if not target_dir.exists() or not target_dir.is_dir():
|
||||
return {"files": [], "total_chars": 0, "file_count": 0, "directory_tree": ""}
|
||||
|
||||
# Check cache
|
||||
file_mtimes: list[tuple[str, float]] = []
|
||||
md_files: list[Path] = []
|
||||
try:
|
||||
for p in target_dir.rglob("*"):
|
||||
# Skip hidden dirs/files and special dirs
|
||||
parts = p.relative_to(target_dir).parts
|
||||
if any(_should_skip(part) for part in parts):
|
||||
continue
|
||||
if p.is_file() and p.suffix.lower() == ".md":
|
||||
md_files.append(p)
|
||||
try:
|
||||
file_mtimes.append((str(p.relative_to(target_dir)), p.stat().st_mtime))
|
||||
except OSError:
|
||||
file_mtimes.append((str(p.relative_to(target_dir)), 0.0))
|
||||
except PermissionError:
|
||||
logger.warning(f"Permission denied scanning {target_dir}")
|
||||
return {"files": [], "total_chars": 0, "file_count": 0, "directory_tree": ""}
|
||||
|
||||
key = _cache_key(vault_resolved, directory, file_mtimes)
|
||||
if key in _cache:
|
||||
cached = _cache[key]
|
||||
if time.time() - cached.get("_ts", 0) < _CACHE_TTL:
|
||||
logger.debug(f"Cache hit for {directory}")
|
||||
return {k: v for k, v in cached.items() if k != "_ts"}
|
||||
|
||||
# Sort by priority: README/index first, then by mtime descending
|
||||
md_files.sort(key=_file_priority)
|
||||
|
||||
# Apply limits
|
||||
collected: list[dict[str, Any]] = []
|
||||
total_chars = 0
|
||||
|
||||
for p in md_files:
|
||||
if len(collected) >= BOOKSLM_MAX_FILES:
|
||||
break
|
||||
if total_chars >= BOOKSLM_MAX_TOTAL_CHARS:
|
||||
break
|
||||
|
||||
rel_path = str(p.relative_to(vault_resolved)).replace("\\", "/")
|
||||
try:
|
||||
content = p.read_text(encoding="utf-8", errors="replace")
|
||||
except Exception as e:
|
||||
logger.warning(f"Cannot read {rel_path}: {e}")
|
||||
continue
|
||||
|
||||
# Redact secrets
|
||||
content = redact_file_content(content, rel_path)
|
||||
|
||||
# Truncate if too long
|
||||
if len(content) > BOOKSLM_MAX_FILE_CHARS:
|
||||
content = content[:BOOKSLM_MAX_FILE_CHARS] + "\n\n[... tronqué]"
|
||||
|
||||
remaining = BOOKSLM_MAX_TOTAL_CHARS - total_chars
|
||||
if len(content) > remaining:
|
||||
content = content[:remaining] + "\n\n[... tronqué]"
|
||||
|
||||
title = p.stem.replace("-", " ").replace("_", " ").title()
|
||||
file_type = "markdown"
|
||||
|
||||
collected.append({
|
||||
"path": rel_path,
|
||||
"title": title,
|
||||
"content": content,
|
||||
"type": file_type,
|
||||
})
|
||||
total_chars += len(content)
|
||||
|
||||
# Build directory tree
|
||||
dir_tree = _build_directory_tree(target_dir, vault_resolved)
|
||||
|
||||
result = {
|
||||
"files": collected,
|
||||
"total_chars": total_chars,
|
||||
"file_count": len(collected),
|
||||
"directory_tree": dir_tree,
|
||||
"max_total_chars": BOOKSLM_MAX_TOTAL_CHARS,
|
||||
"max_files": BOOKSLM_MAX_FILES,
|
||||
"scope": "directory",
|
||||
}
|
||||
|
||||
# Store in cache
|
||||
_cache[key] = {**result, "_ts": time.time()}
|
||||
logger.info(f"Collected {len(collected)} files ({total_chars} chars) from {directory or '/'}")
|
||||
return result
|
||||
|
||||
|
||||
def _file_entry(target: Path, rel_path: str, remaining: int) -> dict[str, Any] | None:
|
||||
"""Read, redact and truncate a single file into a context entry."""
|
||||
suffix = target.suffix.lower()
|
||||
try:
|
||||
if suffix == ".pdf":
|
||||
from backend.pdf_reader import extract_pdf_text
|
||||
|
||||
content = extract_pdf_text(target)
|
||||
file_type = "pdf"
|
||||
else:
|
||||
content = target.read_text(encoding="utf-8", errors="replace")
|
||||
file_type = "markdown"
|
||||
except Exception as e:
|
||||
logger.warning(f"Cannot read {rel_path}: {e}")
|
||||
return None
|
||||
|
||||
content = redact_file_content(content, rel_path)
|
||||
if len(content) > BOOKSLM_MAX_FILE_CHARS:
|
||||
content = content[:BOOKSLM_MAX_FILE_CHARS] + "\n\n[... tronqué]"
|
||||
if len(content) > remaining:
|
||||
content = content[:remaining] + "\n\n[... tronqué]"
|
||||
|
||||
return {
|
||||
"path": rel_path,
|
||||
"title": target.stem.replace("-", " ").replace("_", " ").title(),
|
||||
"content": content,
|
||||
"type": file_type,
|
||||
}
|
||||
|
||||
|
||||
def collect_files_context(
|
||||
vault_path: Path,
|
||||
rel_paths: list[str],
|
||||
scope: str = "documents",
|
||||
) -> dict[str, Any]:
|
||||
"""Collect an explicit list of files (open documents) as AI context.
|
||||
|
||||
Unlike :func:`collect_directory_context`, this reads only the requested
|
||||
relative paths (markdown or PDF), never the whole directory. Paths outside
|
||||
the vault are silently ignored.
|
||||
|
||||
Args:
|
||||
vault_path: Absolute path to the vault root.
|
||||
rel_paths: Relative file paths within the vault (in display order).
|
||||
scope: Context label exposed to the UI ("documents").
|
||||
|
||||
Returns:
|
||||
Same shape as :func:`collect_directory_context`.
|
||||
"""
|
||||
vault_resolved = vault_path.resolve()
|
||||
collected: list[dict[str, Any]] = []
|
||||
total_chars = 0
|
||||
seen: set[str] = set()
|
||||
|
||||
for rel in rel_paths:
|
||||
if len(collected) >= BOOKSLM_MAX_FILES or total_chars >= BOOKSLM_MAX_TOTAL_CHARS:
|
||||
break
|
||||
if not rel or rel in seen:
|
||||
continue
|
||||
seen.add(rel)
|
||||
try:
|
||||
target = (vault_resolved / rel).resolve()
|
||||
target.relative_to(vault_resolved)
|
||||
except (ValueError, OSError):
|
||||
continue
|
||||
if not target.is_file():
|
||||
continue
|
||||
entry = _file_entry(target, rel, BOOKSLM_MAX_TOTAL_CHARS - total_chars)
|
||||
if entry is None:
|
||||
continue
|
||||
collected.append(entry)
|
||||
total_chars += len(entry["content"])
|
||||
|
||||
return {
|
||||
"files": collected,
|
||||
"total_chars": total_chars,
|
||||
"file_count": len(collected),
|
||||
"directory_tree": "",
|
||||
"max_total_chars": BOOKSLM_MAX_TOTAL_CHARS,
|
||||
"max_files": BOOKSLM_MAX_FILES,
|
||||
"scope": scope,
|
||||
}
|
||||
|
||||
|
||||
def collect_adhoc_context(
|
||||
vault_path: Path,
|
||||
files: list[str] | None = None,
|
||||
directories: list[str] | None = None,
|
||||
scope: str = "general",
|
||||
) -> dict[str, Any]:
|
||||
"""Collect an ad-hoc mix of explicit files and directories.
|
||||
|
||||
Backs the assistant ``@`` command: the user can attach extra files and
|
||||
directories to the current context without changing the base mode. Paths
|
||||
outside the vault are ignored by the underlying collectors.
|
||||
|
||||
Args:
|
||||
vault_path: Absolute path to the vault root.
|
||||
files: Relative file paths to include.
|
||||
directories: Relative directory paths to include (recursive).
|
||||
scope: Context label exposed to the UI.
|
||||
|
||||
Returns:
|
||||
Same shape as :func:`collect_directory_context`.
|
||||
"""
|
||||
vault_resolved = vault_path.resolve()
|
||||
collected: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
total_chars = 0
|
||||
|
||||
def _add(entries: list[dict[str, Any]]) -> None:
|
||||
nonlocal total_chars
|
||||
for entry in entries:
|
||||
if len(collected) >= BOOKSLM_MAX_FILES or total_chars >= BOOKSLM_MAX_TOTAL_CHARS:
|
||||
return
|
||||
path = entry.get("path")
|
||||
if not path or path in seen:
|
||||
continue
|
||||
seen.add(path)
|
||||
collected.append(entry)
|
||||
total_chars += len(entry.get("content", ""))
|
||||
|
||||
if files:
|
||||
_add(collect_files_context(vault_resolved, files, scope=scope)["files"])
|
||||
for directory in directories or []:
|
||||
_add(collect_directory_context(vault_resolved, directory)["files"])
|
||||
|
||||
return {
|
||||
"files": collected,
|
||||
"total_chars": total_chars,
|
||||
"file_count": len(collected),
|
||||
"directory_tree": "",
|
||||
"max_total_chars": BOOKSLM_MAX_TOTAL_CHARS,
|
||||
"max_files": BOOKSLM_MAX_FILES,
|
||||
"scope": scope,
|
||||
}
|
||||
|
||||
|
||||
def merge_contexts(base: dict[str, Any], extra: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Merge two context payloads, de-duplicating files by path."""
|
||||
files: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
total_chars = 0
|
||||
for entry in list(base.get("files", [])) + list(extra.get("files", [])):
|
||||
path = entry.get("path")
|
||||
if not path or path in seen:
|
||||
continue
|
||||
if len(files) >= BOOKSLM_MAX_FILES or total_chars >= BOOKSLM_MAX_TOTAL_CHARS:
|
||||
break
|
||||
seen.add(path)
|
||||
files.append(entry)
|
||||
total_chars += len(entry.get("content", ""))
|
||||
|
||||
tree = base.get("directory_tree", "") or extra.get("directory_tree", "")
|
||||
scope = extra.get("scope") or base.get("scope", "general")
|
||||
return {
|
||||
"files": files,
|
||||
"total_chars": total_chars,
|
||||
"file_count": len(files),
|
||||
"directory_tree": tree,
|
||||
"max_total_chars": BOOKSLM_MAX_TOTAL_CHARS,
|
||||
"max_files": BOOKSLM_MAX_FILES,
|
||||
"scope": scope,
|
||||
}
|
||||
|
||||
|
||||
def is_image_path(path: str) -> bool:
|
||||
"""True when the path has a supported image extension."""
|
||||
return Path(path or "").suffix.lower() in IMAGE_EXTENSIONS
|
||||
|
||||
|
||||
def load_vault_image_data_url(vault_path: Path, rel_path: str) -> str | None:
|
||||
"""Read a vault image and return it as a ``data:`` URL for vision models.
|
||||
|
||||
Returns ``None`` when the path is outside the vault, missing, too large,
|
||||
or not a supported image.
|
||||
"""
|
||||
if not rel_path or not is_image_path(rel_path):
|
||||
return None
|
||||
vault_resolved = vault_path.resolve()
|
||||
try:
|
||||
target = (vault_resolved / rel_path).resolve()
|
||||
target.relative_to(vault_resolved)
|
||||
except (ValueError, OSError):
|
||||
return None
|
||||
if not target.is_file():
|
||||
return None
|
||||
try:
|
||||
if target.stat().st_size > BOOKSLM_MAX_IMAGE_BYTES:
|
||||
logger.warning("Image too large to send: %s", rel_path)
|
||||
return None
|
||||
raw = target.read_bytes()
|
||||
except OSError as exc:
|
||||
logger.warning("Cannot read image %s: %s", rel_path, exc)
|
||||
return None
|
||||
mime = mimetypes.guess_type(str(target))[0] or "image/png"
|
||||
encoded = base64.b64encode(raw).decode("ascii")
|
||||
return f"data:{mime};base64,{encoded}"
|
||||
|
||||
|
||||
def empty_context(scope: str = "general") -> dict[str, Any]:
|
||||
"""Return an empty context payload (used by the General assistant)."""
|
||||
return {
|
||||
"files": [],
|
||||
"total_chars": 0,
|
||||
"file_count": 0,
|
||||
"directory_tree": "",
|
||||
"max_total_chars": BOOKSLM_MAX_TOTAL_CHARS,
|
||||
"max_files": BOOKSLM_MAX_FILES,
|
||||
"scope": scope,
|
||||
}
|
||||
|
||||
|
||||
def _build_directory_tree(target_dir: Path, vault_root: Path) -> str:
|
||||
"""Build a text representation of the directory tree (dirs + .md files)."""
|
||||
lines: list[str] = []
|
||||
try:
|
||||
for p in sorted(target_dir.rglob("*")):
|
||||
parts = p.relative_to(target_dir).parts
|
||||
if any(_should_skip(part) for part in parts):
|
||||
continue
|
||||
if p.is_dir():
|
||||
depth = len(p.relative_to(target_dir).parts)
|
||||
lines.append(f"{' ' * depth}{p.name}/")
|
||||
elif p.is_file() and p.suffix.lower() == ".md":
|
||||
depth = len(p.relative_to(target_dir).parts)
|
||||
lines.append(f"{' ' * depth}{p.name}")
|
||||
except PermissionError:
|
||||
pass
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def build_system_prompt(context: dict[str, Any], scope: str = "directory", vault_name: str | None = None) -> str:
|
||||
"""Build a system prompt for document-scoped AI chat.
|
||||
|
||||
Args:
|
||||
context: Output of collect_directory_context()/collect_files_context().
|
||||
scope: "directory" (whole folder) or "documents" (open files).
|
||||
vault_name: Vault the context files belong to. When set, the prompt
|
||||
states it explicitly with write-tool guidance (BUG-046: without
|
||||
it the model invented vault names — e.g. "test" — and every
|
||||
confirmed ``append_to_file``/``edit_file`` call failed).
|
||||
|
||||
Returns:
|
||||
System prompt string with file contents.
|
||||
"""
|
||||
files = context.get("files", [])
|
||||
file_count = context.get("file_count", 0)
|
||||
total_chars = context.get("total_chars", 0)
|
||||
|
||||
# Rough token estimate (1 token ≈ 4 chars)
|
||||
est_tokens = total_chars // 4
|
||||
token_warning = ""
|
||||
if est_tokens > 100_000:
|
||||
token_warning = f"\n⚠️ Attention : le contexte est très volumineux (~{est_tokens:,} tokens estimés). Les réponses peuvent être moins précises.\n"
|
||||
|
||||
if scope == "documents":
|
||||
role = (
|
||||
"Tu es un assistant de recherche documentaire intégré à ObsiGate. "
|
||||
"Tu réponds UNIQUEMENT en te basant sur les documents ouverts par l'utilisateur ci-dessous. "
|
||||
"Cite tes sources avec le nom du fichier quand tu utilises une information. "
|
||||
"Si l'information ne se trouve pas dans les documents, dis-le clairement."
|
||||
)
|
||||
context_label = "📄 Documents ouverts"
|
||||
else:
|
||||
role = (
|
||||
"Tu es un assistant de recherche documentaire. "
|
||||
"Tu réponds UNIQUEMENT en te basant sur les documents fournis ci-dessous. "
|
||||
"Cite tes sources avec le nom du fichier quand tu utilises une information. "
|
||||
"Si l'information ne se trouve pas dans les documents, dis-le clairement."
|
||||
)
|
||||
context_label = "📚 Contexte"
|
||||
|
||||
prompt = (
|
||||
f"{role}"
|
||||
f"\n\n{context_label} : {file_count} fichier(s) ({total_chars:,} caractères)"
|
||||
f"{token_warning}\n"
|
||||
)
|
||||
|
||||
# Directory tree
|
||||
tree = context.get("directory_tree", "")
|
||||
if tree:
|
||||
prompt += f"\n📂 Arborescence du dossier :\n```\n{tree}\n```\n"
|
||||
|
||||
# File contents
|
||||
prompt += "\n---\n"
|
||||
for f in files:
|
||||
prompt += f"\n## 📄 {f['title']} (`{f['path']}`)\n\n{f['content']}\n\n---\n"
|
||||
|
||||
prompt += "\nFin du contexte. Réponds à la question de l'utilisateur en te basant uniquement sur ces documents."
|
||||
|
||||
if vault_name:
|
||||
prompt += (
|
||||
f"\n\nCes documents appartiennent au vault « {vault_name} ». Quand tu utilises un outil "
|
||||
"d'écriture (`append_to_file`, `edit_file`, `create_file`), passe TOUJOURS "
|
||||
f"exactement `\"vault\": \"{vault_name}\"` (jamais un nom inventé) et un `path` "
|
||||
"relatif au vault, identique à celui affiché ci-dessus."
|
||||
)
|
||||
|
||||
return prompt
|
||||
|
||||
|
||||
GENERAL_SYSTEM_PROMPT = """Tu es l'assistant intégré d'ObsiGate, une application web auto-hébergée pour consulter, rechercher et éditer des vaults Obsidian (Markdown).
|
||||
|
||||
Tes deux rôles :
|
||||
1. **Aider sur l'application** : expliquer la navigation, la recherche (full-text, filtres `tag:`, `created:`, `path:`), l'éditeur (CodeMirror, autosave, raccourcis), les onglets et le split view, les sauvegardes et la restauration, le partage public, l'export (HTML/Markdown/ePub/PDF), Mermaid, Excalidraw, les plugins, les thèmes, le mode hors-ligne, le MFA, etc.
|
||||
2. **Proposer des actions concrètes** : créer un fichier ou un dossier dans un vault.
|
||||
|
||||
Quand l'utilisateur demande explicitement de créer un fichier, inclus EXACTEMENT un bloc de ce type dans ta réponse (et rien d'autre à l'intérieur du bloc) :
|
||||
|
||||
```obsigate-action
|
||||
{"action": "create_file", "vault": "<nom du vault>", "path": "<chemin/relatif.md>", "content": "<contenu markdown>"}
|
||||
```
|
||||
|
||||
Pour créer un dossier :
|
||||
|
||||
```obsigate-action
|
||||
{"action": "create_directory", "vault": "<nom du vault>", "path": "<chemin/relatif>"}
|
||||
```
|
||||
|
||||
Règles :
|
||||
- Ne propose une action que si l'utilisateur la demande explicitement.
|
||||
- Explique en une phrase ce que fait l'action avant le bloc.
|
||||
- Utilise un chemin relatif se terminant par `.md` pour un fichier.
|
||||
- N'invente jamais un nom de vault : utilise l'un des vaults disponibles listés ci-dessous.
|
||||
- Réponds dans la langue de l'utilisateur, de façon concise et structurée (Markdown).
|
||||
"""
|
||||
|
||||
|
||||
def _format_app_context(app_context: dict[str, Any] | None, recent_files: list[dict[str, Any]] | None) -> str:
|
||||
"""Render the live application state for the General assistant prompt.
|
||||
|
||||
The General assistant has no document context; without this block it only
|
||||
knows the app exists. Passing what the user currently sees (open documents,
|
||||
current directory, active search, recently modified files) lets it answer
|
||||
"résume ce que je fais / où j'en suis" style questions.
|
||||
"""
|
||||
app_context = app_context or {}
|
||||
lines: list[str] = []
|
||||
|
||||
vault = app_context.get("vault") or app_context.get("current_vault")
|
||||
if vault:
|
||||
lines.append(f"- Vault sélectionné : {vault}")
|
||||
directory = app_context.get("directory")
|
||||
if directory:
|
||||
lines.append(f"- Répertoire courant : {directory}")
|
||||
current_path = app_context.get("current_path")
|
||||
if current_path:
|
||||
lines.append(f"- Document affiché dans le viewer : {current_path}")
|
||||
|
||||
docs = app_context.get("open_documents") or []
|
||||
rendered_docs = []
|
||||
for doc in docs:
|
||||
if not isinstance(doc, dict) or not doc.get("path"):
|
||||
continue
|
||||
rendered_docs.append(f"{doc['path']} (vault {doc['vault']})" if doc.get("vault") else str(doc["path"]))
|
||||
if rendered_docs:
|
||||
lines.append("- Documents ouverts dans les onglets/panneaux : " + ", ".join(rendered_docs))
|
||||
|
||||
editing = app_context.get("editing")
|
||||
if isinstance(editing, dict) and editing.get("path"):
|
||||
surface = "Forge" if editing.get("surface") == "forge" else "l'éditeur"
|
||||
location = f"{editing['path']} (vault {editing['vault']})" if editing.get("vault") else str(editing["path"])
|
||||
lines.append(
|
||||
f"- Document en cours d'édition dans {surface} : {location} — c'est le document affiché à la "
|
||||
"place de la vue lecture. Pour le mettre à jour, utilise les outils d'écriture "
|
||||
"(`edit_file`, `append_to_file`) : la modification est rechargée automatiquement dans "
|
||||
"l'éditeur et la vue lecture dès l'exécution de l'outil."
|
||||
)
|
||||
|
||||
query = app_context.get("search_query")
|
||||
if query:
|
||||
total = app_context.get("search_total")
|
||||
suffix = f" ({total} résultat(s))" if isinstance(total, int) else ""
|
||||
lines.append(f"- Recherche en cours : « {query} »{suffix}")
|
||||
|
||||
results = app_context.get("search_results") or []
|
||||
rendered_results = [str(r.get("path")) for r in results[:10] if isinstance(r, dict) and r.get("path")]
|
||||
if rendered_results:
|
||||
lines.append("- Premiers résultats affichés : " + ", ".join(rendered_results))
|
||||
|
||||
if recent_files:
|
||||
rendered_recent = [
|
||||
f"{f.get('vault')}/{f.get('path')}" for f in recent_files[:10] if f.get("path")
|
||||
]
|
||||
if rendered_recent:
|
||||
lines.append("- Fichiers récemment modifiés : " + ", ".join(rendered_recent))
|
||||
|
||||
if not lines:
|
||||
return ""
|
||||
return (
|
||||
"## Contexte applicatif actuel\n"
|
||||
"Voici ce que l'utilisateur voit ou fait en ce moment dans ObsiGate. "
|
||||
"Sers-t'en pour comprendre sa demande ; ne le répète pas inutilement.\n"
|
||||
+ "\n".join(lines)
|
||||
+ "\n"
|
||||
)
|
||||
|
||||
|
||||
def build_general_system_prompt(
|
||||
vaults: list[str] | None = None,
|
||||
app_context: dict[str, Any] | None = None,
|
||||
recent_files: list[dict[str, Any]] | None = None,
|
||||
) -> str:
|
||||
"""System prompt for the General assistant (app help + actions).
|
||||
|
||||
``app_context`` carries the live UI state (open documents, current
|
||||
directory, active search) and ``recent_files`` the last modified files, so
|
||||
the assistant knows what the user is doing rather than answering blind.
|
||||
"""
|
||||
prompt = GENERAL_SYSTEM_PROMPT
|
||||
if vaults:
|
||||
prompt += "\nVaults disponibles : " + ", ".join(sorted(vaults)) + "\n"
|
||||
else:
|
||||
prompt += "\nAucun vault n'est actuellement configuré.\n"
|
||||
block = _format_app_context(app_context, recent_files)
|
||||
if block:
|
||||
prompt += "\n" + block
|
||||
return prompt
|
||||
|
||||
|
||||
def invalidate_cache(vault_path: Path | None = None, directory: str | None = None) -> int:
|
||||
"""Invalidate cache entries.
|
||||
|
||||
Args:
|
||||
vault_path: If provided, only invalidate entries for this vault.
|
||||
directory: If provided, only invalidate entries for this directory.
|
||||
|
||||
Returns:
|
||||
Number of cache entries removed.
|
||||
"""
|
||||
if vault_path is None and directory is None:
|
||||
count = len(_cache)
|
||||
_cache.clear()
|
||||
return count
|
||||
|
||||
to_remove = list(_cache)
|
||||
for k in to_remove:
|
||||
del _cache[k]
|
||||
return len(to_remove)
|
||||
@@ -0,0 +1,691 @@
|
||||
"""BooksLM API routes — directory-scoped AI chat for Obsidian vaults."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.agent.loop import run_agent
|
||||
from backend.ai_chat import chat_completion, stream_completion
|
||||
from backend.ai_history import delete_session, get_session, list_sessions, upsert_session
|
||||
from backend.auth.middleware import check_vault_access, require_auth
|
||||
from backend.bookslm import (
|
||||
build_general_system_prompt,
|
||||
build_system_prompt,
|
||||
collect_adhoc_context,
|
||||
collect_directory_context,
|
||||
collect_files_context,
|
||||
empty_context,
|
||||
load_vault_image_data_url,
|
||||
merge_contexts,
|
||||
)
|
||||
from backend.indexer import get_vault_data, index
|
||||
from backend.model_capabilities import model_supports_vision
|
||||
from backend.schemas import BooksLMContextResponse
|
||||
from backend.skills import get_skill_prompt
|
||||
from backend.tools.api import ToolContext, ToolMode
|
||||
|
||||
logger = logging.getLogger("obsigate.bookslm_routes")
|
||||
router = APIRouter(prefix="/api/ai/bookslm", tags=["BooksLM"])
|
||||
|
||||
VALID_MODES = {"directory", "documents", "general"}
|
||||
|
||||
|
||||
# ── Request models ──
|
||||
|
||||
|
||||
class BooksLMContextRequest(BaseModel):
|
||||
vault: str | None = Field(default=None, description="Vault name (required for directory/documents modes)")
|
||||
directory: str = Field(default="", description="Relative directory path within the vault")
|
||||
mode: str = Field(default="directory", description="Context mode: 'directory', 'documents' or 'general'")
|
||||
context_files: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Relative file paths to use as context (documents mode)",
|
||||
)
|
||||
extra_files: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Ad-hoc files added with the '@' command (any mode)",
|
||||
)
|
||||
extra_directories: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Ad-hoc directories added with the '@' command (any mode)",
|
||||
)
|
||||
app_context: dict[str, Any] | None = Field(
|
||||
default=None,
|
||||
description="Live client UI state for the General assistant: open_documents, "
|
||||
"current_path, directory, vault, search_query, search_total, search_results.",
|
||||
)
|
||||
|
||||
|
||||
class BooksLMChatRequest(BaseModel):
|
||||
vault: str | None = Field(default=None, description="Vault name (required for directory/documents modes)")
|
||||
directory: str = Field(default="", description="Relative directory path within the vault")
|
||||
mode: str = Field(default="directory", description="Context mode: 'directory', 'documents' or 'general'")
|
||||
context_files: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Relative file paths to use as context (documents mode)",
|
||||
)
|
||||
extra_files: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Ad-hoc files added with the '@' command (any mode)",
|
||||
)
|
||||
extra_directories: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="Ad-hoc directories added with the '@' command (any mode)",
|
||||
)
|
||||
message: str = Field(description="User message")
|
||||
images: list[dict[str, Any]] = Field(
|
||||
default_factory=list,
|
||||
description="Images for vision models. Each item: {data, mime_type} (pasted) "
|
||||
"or {path} (vault-relative file).",
|
||||
)
|
||||
skill: str | None = Field(
|
||||
default=None,
|
||||
description="Skill id selected with the '/' command; its prompt is added to the system prompt.",
|
||||
)
|
||||
conversation_history: list[dict[str, str]] = Field(
|
||||
default_factory=list,
|
||||
description="Previous conversation turns [{role, content}]",
|
||||
)
|
||||
provider: str | None = Field(
|
||||
default=None,
|
||||
description="AI provider override (e.g. 'deepseek', 'openrouter', 'gemini', 'nvidia', 'xiaomi', 'mistral', 'qwencloud'). "
|
||||
"If not set, uses DEFAULT_PROVIDER.",
|
||||
)
|
||||
model: str | None = Field(
|
||||
default=None,
|
||||
description="Model name to use for this request. If not set, uses the provider's default model.",
|
||||
)
|
||||
confirm: dict[str, Any] | None = Field(
|
||||
default=None,
|
||||
description="Pending tool confirmation to apply (two-step propose/apply). "
|
||||
"Shape: the ``error`` object of a previous ``confirmation`` event.",
|
||||
)
|
||||
confirm_messages: list[dict[str, Any]] | None = Field(
|
||||
default=None,
|
||||
description="Conversation snapshot returned alongside a ``confirmation`` event, "
|
||||
"echoed back to resume the agent run.",
|
||||
)
|
||||
app_context: dict[str, Any] | None = Field(
|
||||
default=None,
|
||||
description="Live client UI state for the General assistant: open_documents, "
|
||||
"current_path, directory, vault, search_query, search_total, search_results.",
|
||||
)
|
||||
|
||||
|
||||
def _normalize_mode(mode: str | None) -> str:
|
||||
mode = (mode or "directory").lower()
|
||||
return mode if mode in VALID_MODES else "directory"
|
||||
|
||||
|
||||
def _resolve_vault_path(vault: str | None, current_user):
|
||||
"""Validate vault access and return (vault_name, vault_path)."""
|
||||
if not vault:
|
||||
raise HTTPException(status_code=400, detail="Champ 'vault' requis pour ce contexte")
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
vault_data = get_vault_data(vault)
|
||||
if not vault_data:
|
||||
raise HTTPException(status_code=404, detail=f"Vault '{vault}' not found")
|
||||
return vault, Path(vault_data["path"])
|
||||
|
||||
|
||||
def _build_context(
|
||||
mode: str,
|
||||
vault_path: Path | None,
|
||||
directory: str,
|
||||
context_files: list[str],
|
||||
extra_files: list[str] | None = None,
|
||||
extra_directories: list[str] | None = None,
|
||||
):
|
||||
"""Collect the context payload for the requested mode.
|
||||
|
||||
Ad-hoc files/directories (``@`` command) are merged on top of the base
|
||||
context. In General mode, attaching ad-hoc files promotes the effective
|
||||
scope to ``documents`` so the prompt actually includes their content.
|
||||
"""
|
||||
if mode == "general":
|
||||
base = empty_context("general")
|
||||
elif mode == "documents":
|
||||
ctx = collect_files_context(vault_path, context_files, scope="documents") # type: ignore[arg-type]
|
||||
# No open document could be read → degrade gracefully to General.
|
||||
base = ctx if ctx["file_count"] else empty_context("general")
|
||||
else:
|
||||
base = collect_directory_context(vault_path, directory) # type: ignore[arg-type]
|
||||
|
||||
if (extra_files or extra_directories) and vault_path is not None:
|
||||
adhoc_scope = "documents" if base.get("scope") == "general" else base.get("scope", "directory")
|
||||
adhoc = collect_adhoc_context(vault_path, extra_files, extra_directories, scope=adhoc_scope)
|
||||
if adhoc["file_count"]:
|
||||
base = merge_contexts(base, adhoc)
|
||||
return base
|
||||
|
||||
|
||||
def _submitted_app_context(req) -> dict[str, Any] | None:
|
||||
"""Return the client-submitted live app state (may be None)."""
|
||||
ctx = getattr(req, "app_context", None)
|
||||
return ctx if isinstance(ctx, dict) else None
|
||||
|
||||
|
||||
def _recent_files_for_prompt(current_user, limit: int = 10) -> list[dict[str, Any]]:
|
||||
"""Best-effort list of the user's most recently modified files.
|
||||
|
||||
Used to give the General assistant a sense of what the user has been
|
||||
working on. Never raises: any failure yields an empty list.
|
||||
"""
|
||||
try:
|
||||
from backend.services.recent import list_recent
|
||||
|
||||
username = current_user.get("username") if isinstance(current_user, dict) else None
|
||||
user_vaults = (
|
||||
current_user.get("_token_vaults") or current_user.get("vaults", [])
|
||||
if isinstance(current_user, dict)
|
||||
else []
|
||||
)
|
||||
data = list_recent(username, user_vaults, limit=limit, mode="modified")
|
||||
return list(data.get("files", []))
|
||||
except Exception: # pragma: no cover - defensive, prompt enrichment only
|
||||
logger.debug("Could not gather recent files for assistant prompt", exc_info=True)
|
||||
return []
|
||||
|
||||
|
||||
def _resolve_system_prompt(req, current_user) -> str:
|
||||
"""Resolve the vault access and build the assistant system prompt.
|
||||
|
||||
Shared by the classic chat endpoint and the tool-calling agent endpoint.
|
||||
"""
|
||||
mode = _normalize_mode(req.mode)
|
||||
vault_path: Path | None = None
|
||||
if mode != "general":
|
||||
_, vault_path = _resolve_vault_path(req.vault, current_user)
|
||||
elif getattr(req, "extra_files", None) or getattr(req, "extra_directories", None):
|
||||
# General mode has no base context, but ad-hoc files/directories added
|
||||
# with `@` still need a vault to be read from.
|
||||
vault_path = _resolve_optional_vault_path(req, current_user)
|
||||
|
||||
context = _build_context(
|
||||
mode,
|
||||
vault_path,
|
||||
req.directory,
|
||||
req.context_files,
|
||||
getattr(req, "extra_files", None),
|
||||
getattr(req, "extra_directories", None),
|
||||
)
|
||||
effective_mode = context.get("scope", mode)
|
||||
|
||||
if effective_mode == "general":
|
||||
prompt = build_general_system_prompt(
|
||||
list(index.keys()),
|
||||
app_context=_submitted_app_context(req),
|
||||
recent_files=_recent_files_for_prompt(current_user),
|
||||
)
|
||||
elif effective_mode == "documents":
|
||||
prompt = build_system_prompt(context, scope="documents", vault_name=req.vault)
|
||||
elif context["file_count"] == 0:
|
||||
# Empty (or unreadable) directory: don't block the request. Answer as
|
||||
# the General assistant would, telling the model the folder is empty so
|
||||
# it can still help (create a file, explain the app, etc.).
|
||||
prompt = build_general_system_prompt(
|
||||
list(index.keys()),
|
||||
app_context=_submitted_app_context(req),
|
||||
recent_files=_recent_files_for_prompt(current_user),
|
||||
)
|
||||
prompt += (
|
||||
f"\n## Dossier vide\nLe dossier « {req.directory or '/'} » "
|
||||
f"(vault {req.vault}) ne contient aucun fichier markdown exploitable. "
|
||||
"Réponds quand même à la demande de l'utilisateur sans contexte "
|
||||
"documentaire, et propose une action de création si c'est pertinent.\n"
|
||||
)
|
||||
else:
|
||||
prompt = build_system_prompt(context, scope="directory", vault_name=req.vault)
|
||||
|
||||
skill_id = getattr(req, "skill", None)
|
||||
if skill_id:
|
||||
skill_prompt = get_skill_prompt(skill_id, current_user)
|
||||
if skill_prompt:
|
||||
prompt += "\n\n## Skill actif\n" + skill_prompt
|
||||
return prompt
|
||||
|
||||
|
||||
def _resolve_optional_vault_path(req, current_user) -> Path | None:
|
||||
"""Best-effort vault path resolution (never raises)."""
|
||||
if not getattr(req, "vault", None):
|
||||
return None
|
||||
try:
|
||||
_, vault_path = _resolve_vault_path(req.vault, current_user)
|
||||
return vault_path
|
||||
except HTTPException:
|
||||
return None
|
||||
|
||||
|
||||
def _build_user_content(req, vault_path: Path | None):
|
||||
"""Build the user message content, attaching images when present.
|
||||
|
||||
Returns a plain string when there is no image, otherwise an OpenAI-style
|
||||
multimodal content array (which ``ai_chat`` adapts for Gemini).
|
||||
"""
|
||||
images = getattr(req, "images", None) or []
|
||||
if not images:
|
||||
return req.message
|
||||
|
||||
parts: list[dict[str, Any]] = [{"type": "text", "text": req.message}]
|
||||
for image in images:
|
||||
if not isinstance(image, dict):
|
||||
continue
|
||||
data_url: str | None = None
|
||||
if image.get("data"):
|
||||
mime = image.get("mime_type") or "image/png"
|
||||
data_url = f"data:{mime};base64,{image['data']}"
|
||||
elif image.get("path") and vault_path is not None:
|
||||
data_url = load_vault_image_data_url(vault_path, str(image["path"]))
|
||||
if data_url:
|
||||
parts.append({"type": "image_url", "image_url": {"url": data_url}})
|
||||
return parts
|
||||
|
||||
|
||||
def _validate_vision_support(req) -> None:
|
||||
"""Reject image requests when the selected model cannot analyse images."""
|
||||
if not (getattr(req, "images", None)):
|
||||
return
|
||||
provider = _resolve_provider_name(req.provider)
|
||||
if not provider:
|
||||
return
|
||||
from backend.ai import PROVIDERS
|
||||
|
||||
model = req.model or PROVIDERS.get(provider, {}).get("model", "")
|
||||
if not model_supports_vision(provider, model):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Le modèle '{model or provider}' ne supporte pas l'analyse d'images. "
|
||||
"Choisissez un modèle compatible vision.",
|
||||
)
|
||||
|
||||
|
||||
def _resolve_provider_name(requested: str | None) -> str | None:
|
||||
"""Pick the provider to use: explicit override, else first available."""
|
||||
from backend.ai import DEFAULT_PROVIDER, PROVIDERS
|
||||
|
||||
cfg_name = (requested or DEFAULT_PROVIDER).lower()
|
||||
if cfg_name in PROVIDERS and PROVIDERS[cfg_name].get("api_key"):
|
||||
return cfg_name
|
||||
for pname, pcfg in PROVIDERS.items():
|
||||
if pcfg.get("api_key") and pname != "gemini":
|
||||
return pname
|
||||
return None
|
||||
|
||||
|
||||
def _effective_model(provider: str | None, requested: str | None) -> str:
|
||||
"""Model actually used for a request.
|
||||
|
||||
The client may leave `model` empty (provider default) — reporting the raw
|
||||
request would show nothing in the "provider · model" tag, so the provider's
|
||||
configured default is returned instead.
|
||||
"""
|
||||
if requested:
|
||||
return requested
|
||||
if not provider:
|
||||
return ""
|
||||
from backend.ai import PROVIDERS
|
||||
|
||||
return PROVIDERS.get(provider, {}).get("model", "") or ""
|
||||
|
||||
|
||||
def _tool_sources(rec) -> list[dict[str, str]]:
|
||||
"""Compact web sources of a tool result (rendered as links in the UI).
|
||||
|
||||
Only the web tools produce sources: ``web_search`` returns ranked results,
|
||||
``fetch_url`` a single page. Everything else yields an empty list so the
|
||||
SSE payload stays small.
|
||||
"""
|
||||
data = rec.result if isinstance(rec.result, dict) else {}
|
||||
name = rec.name or ""
|
||||
sources: list[dict[str, str]] = []
|
||||
if name == "web_search":
|
||||
for item in (data.get("results") or [])[:8]:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
url = item.get("url") or ""
|
||||
if not url:
|
||||
continue
|
||||
sources.append({"title": item.get("title") or url, "url": url})
|
||||
elif name == "fetch_url":
|
||||
url = data.get("url") or ""
|
||||
if url:
|
||||
sources.append({"title": data.get("title") or url, "url": url})
|
||||
return sources
|
||||
|
||||
|
||||
def _tool_event_sse(rec) -> str:
|
||||
"""Serialize one executed tool call as an SSE ``tool`` event."""
|
||||
payload = json.dumps(
|
||||
{
|
||||
"name": rec.name,
|
||||
"ok": rec.ok,
|
||||
"arguments": rec.arguments,
|
||||
"step": rec.step,
|
||||
"sources": _tool_sources(rec),
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
return f"event: tool\ndata: {payload}\n\n"
|
||||
|
||||
|
||||
def _thought_event_sse(note: dict) -> str:
|
||||
"""Serialize one intermediate reasoning note as an SSE ``step`` event."""
|
||||
payload = json.dumps({"step": note}, ensure_ascii=False)
|
||||
return f"event: step\ndata: {payload}\n\n"
|
||||
|
||||
|
||||
# ── Endpoints ──
|
||||
|
||||
|
||||
@router.post("/context", response_model=BooksLMContextResponse)
|
||||
async def api_bookslm_context(
|
||||
req: BooksLMContextRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Collect context for the AI assistant.
|
||||
|
||||
Three modes are supported:
|
||||
|
||||
* ``directory`` — every supported file under a vault directory;
|
||||
* ``documents`` — only the explicitly listed open documents;
|
||||
* ``general`` — no document context (assistant for the app itself).
|
||||
"""
|
||||
mode = _normalize_mode(req.mode)
|
||||
if mode == "general":
|
||||
if not (req.extra_files or req.extra_directories):
|
||||
return empty_context("general")
|
||||
vault_path = _resolve_optional_vault_path(req, current_user)
|
||||
else:
|
||||
_, vault_path = _resolve_vault_path(req.vault, current_user)
|
||||
return _build_context(
|
||||
mode, vault_path, req.directory, req.context_files,
|
||||
req.extra_files, req.extra_directories,
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/chat",
|
||||
response_class=StreamingResponse,
|
||||
responses={200: {"content": {"text/event-stream": {}}, "description": "SSE token stream"}},
|
||||
)
|
||||
async def api_bookslm_chat(
|
||||
req: BooksLMChatRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Chat with the AI assistant.
|
||||
|
||||
Builds a system prompt from the selected context (directory, open
|
||||
documents or general app knowledge), then streams the provider's answer
|
||||
as Server-Sent Events.
|
||||
"""
|
||||
_validate_vision_support(req)
|
||||
system_prompt = _resolve_system_prompt(req, current_user)
|
||||
vault_path = _resolve_optional_vault_path(req, current_user)
|
||||
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": system_prompt}]
|
||||
for turn in req.conversation_history:
|
||||
role = turn.get("role")
|
||||
content = turn.get("content", "")
|
||||
if role in ("user", "assistant") and content:
|
||||
messages.append({"role": role, "content": content})
|
||||
messages.append({"role": "user", "content": _build_user_content(req, vault_path)})
|
||||
|
||||
async def generate_sse():
|
||||
try:
|
||||
# Resolve provider: explicit override wins, else first available.
|
||||
cfg_name = _resolve_provider_name(req.provider)
|
||||
if cfg_name is None:
|
||||
err = "Aucun fournisseur AI configuré (clés API manquantes)"
|
||||
error_data = json.dumps({"error": err}, ensure_ascii=False)
|
||||
yield f"event: error\ndata: {error_data}\n\n"
|
||||
return
|
||||
|
||||
# Stream token deltas as they arrive from the provider.
|
||||
async for token in stream_completion(
|
||||
messages,
|
||||
provider=cfg_name,
|
||||
model=req.model,
|
||||
temperature=0.3,
|
||||
max_tokens=4096,
|
||||
):
|
||||
data = json.dumps(
|
||||
{
|
||||
"token": token,
|
||||
"provider": cfg_name,
|
||||
"model": _effective_model(cfg_name, req.model),
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
yield f"event: message\ndata: {data}\n\n"
|
||||
yield "event: done\ndata: {}\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"BooksLM chat error: {e}")
|
||||
error_data = json.dumps({"error": str(e)}, ensure_ascii=False)
|
||||
yield f"event: error\ndata: {error_data}\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
generate_sse(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/agent",
|
||||
response_class=StreamingResponse,
|
||||
responses={200: {"content": {"text/event-stream": {}}, "description": "SSE tool/agent stream"}},
|
||||
)
|
||||
async def api_bookslm_agent(
|
||||
req: BooksLMChatRequest,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Chat with the tool-calling agent.
|
||||
|
||||
Same context as ``/chat`` but the model may call tools (read/search the
|
||||
vault) through the shared tool layer. Emits one ``tool`` event per executed
|
||||
tool call, then a final ``message`` event. Mutating tools pause the run with
|
||||
a ``confirmation`` event (two-step propose/apply) carrying the pending call
|
||||
and the conversation snapshot; the client resumes by echoing them back in
|
||||
``confirm`` / ``confirm_messages``.
|
||||
"""
|
||||
_validate_vision_support(req)
|
||||
system_prompt = _resolve_system_prompt(req, current_user)
|
||||
vault_path = _resolve_optional_vault_path(req, current_user)
|
||||
|
||||
messages: list[dict] = [{"role": "system", "content": system_prompt}]
|
||||
for turn in req.conversation_history:
|
||||
role = turn.get("role")
|
||||
content = turn.get("content", "")
|
||||
if role in ("user", "assistant") and content:
|
||||
messages.append({"role": role, "content": content})
|
||||
messages.append({"role": "user", "content": _build_user_content(req, vault_path)})
|
||||
|
||||
ctx = ToolContext(user=current_user, mode=ToolMode.IN_APP)
|
||||
|
||||
async def _llm(msgs, tool_schemas):
|
||||
return await chat_completion(
|
||||
msgs,
|
||||
tools=tool_schemas,
|
||||
provider=req.provider,
|
||||
model=req.model,
|
||||
temperature=0.3,
|
||||
max_tokens=4096,
|
||||
)
|
||||
|
||||
async def generate_sse():
|
||||
import asyncio
|
||||
|
||||
try:
|
||||
cfg_name = _resolve_provider_name(req.provider)
|
||||
if cfg_name is None:
|
||||
error_data = json.dumps(
|
||||
{"error": "Aucun fournisseur AI configuré (clés API manquantes)"},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
yield f"event: error\ndata: {error_data}\n\n"
|
||||
return
|
||||
|
||||
# Stream tool events live: each executed step is pushed on the
|
||||
# queue by the loop callback and emitted as soon as it happens,
|
||||
# so the UI can grow its « N steps » block while thinking.
|
||||
queue: asyncio.Queue = asyncio.Queue()
|
||||
|
||||
def _on_tool(rec) -> None:
|
||||
queue.put_nowait(("tool", rec))
|
||||
|
||||
run_task = asyncio.create_task(run_agent(
|
||||
messages,
|
||||
ctx=ctx,
|
||||
llm=_llm,
|
||||
resume_messages=req.confirm_messages,
|
||||
confirm_pending=req.confirm,
|
||||
on_tool_call=_on_tool,
|
||||
on_thought=lambda note: queue.put_nowait(("thought", note)),
|
||||
))
|
||||
|
||||
# Drain every completed step as soon as it lands, while the agent
|
||||
# keeps running in the background. If the client disconnects, the
|
||||
# generator is cancelled: release the run so it cannot orphan.
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
kind, item = await asyncio.wait_for(queue.get(), timeout=0.25)
|
||||
except asyncio.TimeoutError:
|
||||
if run_task.done():
|
||||
break
|
||||
continue
|
||||
yield _tool_event_sse(item) if kind == "tool" else _thought_event_sse(item)
|
||||
while not queue.empty():
|
||||
kind, item = queue.get_nowait()
|
||||
yield _tool_event_sse(item) if kind == "tool" else _thought_event_sse(item)
|
||||
result = run_task.result()
|
||||
finally:
|
||||
if not run_task.done():
|
||||
run_task.cancel()
|
||||
|
||||
if result.stopped == "confirmation_required":
|
||||
pending = json.dumps(
|
||||
{"pending": result.pending or {}, "messages": result.messages},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
yield f"event: confirmation\ndata: {pending}\n\n"
|
||||
else:
|
||||
data = json.dumps(
|
||||
{
|
||||
"token": result.content,
|
||||
"provider": cfg_name,
|
||||
"model": _effective_model(cfg_name, req.model),
|
||||
"iterations": result.iterations,
|
||||
"stopped": result.stopped,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
yield f"event: message\ndata: {data}\n\n"
|
||||
yield "event: done\ndata: {}\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"BooksLM agent error: {e}")
|
||||
error_data = json.dumps({"error": str(e)}, ensure_ascii=False)
|
||||
yield f"event: error\ndata: {error_data}\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
generate_sse(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
# ── Persistent conversation history (#95) ─────────────────────────────
|
||||
|
||||
|
||||
class BookslmSession(BaseModel):
|
||||
"""Full assistant conversation persisted server-side (#95).
|
||||
|
||||
The shape mirrors the client session object so round-tripping is lossless
|
||||
(transient fields such as ``confirmation``/``payload`` are stripped
|
||||
client-side before upload).
|
||||
"""
|
||||
|
||||
id: str = Field(description="Stable session id (s-<ts36>-<rand>)")
|
||||
title: str = Field(default="", description="Derived human title")
|
||||
mode: str = Field(default="general", description="'directory', 'documents' or 'general'")
|
||||
vault: str | None = Field(default=None, description="Vault name for the context")
|
||||
directory: str = Field(default="", description="Relative directory path")
|
||||
documents: list[dict[str, str]] = Field(
|
||||
default_factory=list,
|
||||
description="Open documents for the 'documents' mode",
|
||||
)
|
||||
context: str = Field(default="", description="Client context key of the assistant")
|
||||
createdAt: int | None = Field(default=None, description="Creation ISO ms timestamp")
|
||||
updatedAt: int | None = Field(default=None, description="Last update ISO ms timestamp")
|
||||
messages: list[dict[str, Any]] = Field(
|
||||
default_factory=list,
|
||||
description="Conversation turns [{role, content}]",
|
||||
)
|
||||
|
||||
|
||||
def _session_user(current_user) -> str:
|
||||
return current_user.get("username") if isinstance(current_user, dict) else "" # type: ignore[return-value]
|
||||
|
||||
|
||||
@router.get("/history", response_model=dict[str, list[dict[str, Any]]])
|
||||
async def api_bookslm_history_list(current_user=Depends(require_auth)):
|
||||
"""List the user's assistant conversations (summaries, most recent first).
|
||||
|
||||
Messages are not included to keep the list light; fetch the full
|
||||
conversation with ``GET /history/{id}`` when a session is opened.
|
||||
"""
|
||||
return {"sessions": list_sessions(_session_user(current_user))}
|
||||
|
||||
|
||||
@router.get("/history/{session_id}", response_model=dict[str, Any] | None)
|
||||
async def api_bookslm_history_get(
|
||||
session_id: str,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Return a single full conversation, or 404 when unknown."""
|
||||
session = get_session(_session_user(current_user), session_id)
|
||||
if session is None:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
return session
|
||||
|
||||
|
||||
@router.put("/history/{session_id}", response_model=dict[str, Any])
|
||||
async def api_bookslm_history_upsert(
|
||||
session_id: str,
|
||||
session: BookslmSession,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Create or update a conversation for the current user.
|
||||
|
||||
The body id is forced to the path id so a client never writes under a
|
||||
different key by mistake.
|
||||
"""
|
||||
payload = session.model_dump()
|
||||
payload["id"] = session_id
|
||||
stored = upsert_session(_session_user(current_user), payload)
|
||||
if stored is None:
|
||||
raise HTTPException(status_code=400, detail="Invalid session id")
|
||||
return stored
|
||||
|
||||
|
||||
@router.delete("/history/{session_id}", response_model=dict[str, bool])
|
||||
async def api_bookslm_history_delete(
|
||||
session_id: str,
|
||||
current_user=Depends(require_auth),
|
||||
):
|
||||
"""Delete a conversation for the current user."""
|
||||
return {"ok": delete_session(_session_user(current_user), session_id)}
|
||||
@@ -0,0 +1,378 @@
|
||||
"""Collaboration temps réel — édition simultanée (ROADMAP #62).
|
||||
|
||||
Ce module implémente le cœur serveur de l'édition collaborative :
|
||||
|
||||
* un **relais WebSocket** : une *room* est créée par fichier ouvert
|
||||
(clé ``vault::chemin``) et tous les clients qui éditent le même fichier
|
||||
rejoignent la même room ;
|
||||
* un **relais de mises à jour Yjs** (CRDT) : le serveur ne décode pas le
|
||||
format binaire Yjs, il stocke le journal des mises à jour reçues et le
|
||||
rejoue aux nouveaux arrivants. La fusion sans conflit est assurée côté
|
||||
client par Yjs ;
|
||||
* un **awareness** (curseurs colorés + sélections) relayé entre clients ;
|
||||
* une **persistance différée** : le texte markdown reçu des clients est écrit
|
||||
sur disque après un debounce (2 s par défaut).
|
||||
|
||||
Sécurité : chaque connexion est authentifiée manuellement (les dépendances
|
||||
FastAPI ``Depends`` ne s'exécutent pas pour ``@app.websocket``), puis le
|
||||
chemin est validé via :func:`backend.services.paths.resolve_safe_path` et
|
||||
l'accès à la vault via ``check_vault_access``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import WebSocket
|
||||
from starlette.websockets import WebSocketDisconnect
|
||||
|
||||
logger = logging.getLogger("obsigate.collab")
|
||||
|
||||
#: Délai (secondes) sans modification avant écriture sur disque.
|
||||
SAVE_DEBOUNCE_SECONDS = 2.0
|
||||
|
||||
#: Taille maximale d'une mise à jour Yjs encodée (protection anti-abus).
|
||||
MAX_UPDATE_BYTES = 8 * 1024 * 1024
|
||||
|
||||
#: Taille maximale d'un snapshot texte (protection anti-abus).
|
||||
MAX_TEXT_CHARS = 8 * 1024 * 1024
|
||||
|
||||
#: Palette de couleurs attribuées aux utilisateurs (curseurs + avatars).
|
||||
PEER_COLORS = [
|
||||
"#e6194b", "#3cb44b", "#4363d8", "#f58231", "#911eb4",
|
||||
"#008080", "#9a6324", "#800000", "#808000", "#000075",
|
||||
]
|
||||
|
||||
|
||||
def color_for_index(index: int) -> str:
|
||||
"""Return a deterministic cursor color for a peer index."""
|
||||
return PEER_COLORS[index % len(PEER_COLORS)]
|
||||
|
||||
|
||||
def _b64encode(data: bytes) -> str:
|
||||
return base64.b64encode(data).decode("ascii")
|
||||
|
||||
|
||||
def _b64decode(data: str) -> bytes:
|
||||
return base64.b64decode(data.encode("ascii"))
|
||||
|
||||
|
||||
def authenticate_websocket(websocket: WebSocket) -> dict[str, Any] | None:
|
||||
"""Authenticate a WebSocket connection.
|
||||
|
||||
Mirrors :func:`backend.auth.middleware.get_current_user` but works on the
|
||||
WebSocket scope: the JWT is read from the ``access_token`` cookie (sent
|
||||
automatically by same-origin browsers during the handshake) or, as a
|
||||
fallback, from the ``token`` query parameter.
|
||||
|
||||
Returns the user dict, or ``None`` if authentication fails.
|
||||
"""
|
||||
from backend.auth.jwt_handler import decode_token
|
||||
from backend.auth.middleware import is_auth_enabled
|
||||
from backend.auth.user_store import get_user
|
||||
|
||||
if not is_auth_enabled():
|
||||
return {
|
||||
"username": "anonymous",
|
||||
"display_name": "Anonymous",
|
||||
"role": "admin",
|
||||
"vaults": ["*"],
|
||||
"active": True,
|
||||
"_token_vaults": ["*"],
|
||||
}
|
||||
|
||||
token = websocket.query_params.get("token") or websocket.cookies.get("access_token")
|
||||
if not token:
|
||||
return None
|
||||
|
||||
payload = decode_token(token)
|
||||
if not payload or payload.get("type") != "access":
|
||||
return None
|
||||
|
||||
user = get_user(payload["sub"])
|
||||
if not user or not user.get("active"):
|
||||
return None
|
||||
|
||||
user["_token_vaults"] = payload.get("vaults", [])
|
||||
user["_token_jti"] = payload.get("jti")
|
||||
return user
|
||||
|
||||
|
||||
@dataclass
|
||||
class CollabClient:
|
||||
"""A single WebSocket connection inside a collaboration room."""
|
||||
|
||||
conn_id: int
|
||||
websocket: WebSocket
|
||||
username: str
|
||||
display_name: str
|
||||
color: str
|
||||
y_client_id: int | None = None
|
||||
awareness: dict[str, Any] | None = None
|
||||
|
||||
def peer(self) -> dict[str, Any]:
|
||||
return {
|
||||
"connId": self.conn_id,
|
||||
"clientId": self.y_client_id,
|
||||
"username": self.username,
|
||||
"displayName": self.display_name,
|
||||
"color": self.color,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class CollabRoom:
|
||||
"""State shared by every client editing the same file."""
|
||||
|
||||
vault: str
|
||||
path: str
|
||||
file_path: Path
|
||||
initial_text: str = ""
|
||||
clients: dict[int, CollabClient] = field(default_factory=dict)
|
||||
#: Journal des mises à jour Yjs (binaires) depuis la création de la room.
|
||||
updates: list[bytes] = field(default_factory=list)
|
||||
has_updates: bool = False
|
||||
seed_sent: bool = False
|
||||
pending_text: str | None = None
|
||||
save_task: asyncio.Task | None = None
|
||||
lock: asyncio.Lock = field(default_factory=asyncio.Lock)
|
||||
|
||||
@property
|
||||
def key(self) -> str:
|
||||
return f"{self.vault}::{self.path}"
|
||||
|
||||
def peers(self) -> list[dict[str, Any]]:
|
||||
return [client.peer() for client in self.clients.values()]
|
||||
|
||||
def awareness_snapshot(self) -> list[dict[str, Any]]:
|
||||
return [
|
||||
{"clientId": c.y_client_id, "state": c.awareness}
|
||||
for c in self.clients.values()
|
||||
if c.y_client_id is not None and c.awareness is not None
|
||||
]
|
||||
|
||||
|
||||
class CollabManager:
|
||||
"""Manages collaboration rooms, broadcasting and disk persistence."""
|
||||
|
||||
def __init__(self, save_debounce: float = SAVE_DEBOUNCE_SECONDS) -> None:
|
||||
self._rooms: dict[str, CollabRoom] = {}
|
||||
self._save_debounce = save_debounce
|
||||
self._next_conn_id = 1
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
# -- introspection (used by tests / diagnostics) ------------------------
|
||||
@property
|
||||
def room_count(self) -> int:
|
||||
return len(self._rooms)
|
||||
|
||||
def room_peer_count(self, vault: str, path: str) -> int:
|
||||
room = self._rooms.get(f"{vault}::{path}")
|
||||
return len(room.clients) if room else 0
|
||||
|
||||
def get_room(self, vault: str, path: str) -> CollabRoom | None:
|
||||
return self._rooms.get(f"{vault}::{path}")
|
||||
|
||||
# -- lifecycle ----------------------------------------------------------
|
||||
async def connect(
|
||||
self,
|
||||
websocket: WebSocket,
|
||||
vault: str,
|
||||
path: str,
|
||||
file_path: Path,
|
||||
user: dict[str, Any],
|
||||
) -> None:
|
||||
"""Register *websocket* in the room and relay messages until it closes."""
|
||||
async with self._lock:
|
||||
key = f"{vault}::{path}"
|
||||
room = self._rooms.get(key)
|
||||
if room is None:
|
||||
try:
|
||||
initial_text = file_path.read_text(encoding="utf-8")
|
||||
except (OSError, UnicodeDecodeError):
|
||||
initial_text = ""
|
||||
room = CollabRoom(vault=vault, path=path, file_path=file_path, initial_text=initial_text)
|
||||
self._rooms[key] = room
|
||||
|
||||
conn_id = self._next_conn_id
|
||||
self._next_conn_id += 1
|
||||
client = CollabClient(
|
||||
conn_id=conn_id,
|
||||
websocket=websocket,
|
||||
username=user.get("username", "anonymous"),
|
||||
display_name=user.get("display_name") or user.get("username", "anonymous"),
|
||||
color=color_for_index(conn_id - 1),
|
||||
)
|
||||
room.clients[conn_id] = client
|
||||
|
||||
seed: str | None = None
|
||||
if not room.has_updates and not room.seed_sent:
|
||||
seed = room.initial_text
|
||||
room.seed_sent = True
|
||||
|
||||
await websocket.send_json({
|
||||
"type": "init",
|
||||
"connId": conn_id,
|
||||
"color": client.color,
|
||||
"seed": seed,
|
||||
"updates": [_b64encode(u) for u in room.updates],
|
||||
"peers": room.peers(),
|
||||
"awareness": room.awareness_snapshot(),
|
||||
})
|
||||
await self._broadcast(room, {"type": "peer_joined", "peer": client.peer()}, exclude=conn_id)
|
||||
|
||||
try:
|
||||
while True:
|
||||
raw = await websocket.receive_text()
|
||||
await self._on_message(room, client, raw)
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
except Exception as exc: # pragma: no cover - defensive
|
||||
logger.debug("Collab connection error (%s): %s", room.key, exc)
|
||||
finally:
|
||||
await self.disconnect(room, client)
|
||||
|
||||
async def disconnect(self, room: CollabRoom, client: CollabClient) -> None:
|
||||
"""Remove *client* from *room*, flushing and cleaning up if empty."""
|
||||
async with self._lock:
|
||||
room.clients.pop(client.conn_id, None)
|
||||
empty = not room.clients
|
||||
if empty:
|
||||
await self._flush(room)
|
||||
async with self._lock:
|
||||
# Only delete if nobody rejoined while we were flushing.
|
||||
if not room.clients and self._rooms.get(room.key) is room:
|
||||
if room.save_task:
|
||||
room.save_task.cancel()
|
||||
self._rooms.pop(room.key, None)
|
||||
else:
|
||||
await self._broadcast(
|
||||
room,
|
||||
{
|
||||
"type": "peer_left",
|
||||
"peer": client.peer(),
|
||||
"clientId": client.y_client_id,
|
||||
},
|
||||
)
|
||||
|
||||
async def stop(self) -> None:
|
||||
"""Flush and cancel every room (called on application shutdown)."""
|
||||
async with self._lock:
|
||||
rooms = list(self._rooms.values())
|
||||
self._rooms.clear()
|
||||
for room in rooms:
|
||||
if room.save_task:
|
||||
room.save_task.cancel()
|
||||
await self._flush(room)
|
||||
|
||||
# -- message handling ---------------------------------------------------
|
||||
async def _on_message(self, room: CollabRoom, client: CollabClient, raw: str) -> None:
|
||||
try:
|
||||
message = json.loads(raw)
|
||||
except (ValueError, TypeError):
|
||||
return
|
||||
if not isinstance(message, dict):
|
||||
return
|
||||
|
||||
msg_type = message.get("type")
|
||||
|
||||
if msg_type in ("sync", "update"):
|
||||
encoded = message.get("update")
|
||||
if not isinstance(encoded, str):
|
||||
return
|
||||
try:
|
||||
update = _b64decode(encoded)
|
||||
except (ValueError, TypeError):
|
||||
return
|
||||
if not update or len(update) > MAX_UPDATE_BYTES:
|
||||
return
|
||||
async with self._lock:
|
||||
room.updates.append(update)
|
||||
room.has_updates = True
|
||||
await self._broadcast(
|
||||
room,
|
||||
{"type": "update", "update": encoded, "from": client.conn_id},
|
||||
exclude=client.conn_id,
|
||||
)
|
||||
|
||||
elif msg_type == "awareness":
|
||||
y_client_id = message.get("clientId")
|
||||
state = message.get("state")
|
||||
if not isinstance(y_client_id, int):
|
||||
return
|
||||
client.y_client_id = y_client_id
|
||||
client.awareness = state if isinstance(state, dict) else None
|
||||
await self._broadcast(
|
||||
room,
|
||||
{
|
||||
"type": "awareness",
|
||||
"clientId": y_client_id,
|
||||
"state": client.awareness,
|
||||
"from": client.conn_id,
|
||||
},
|
||||
exclude=client.conn_id,
|
||||
)
|
||||
|
||||
elif msg_type == "text":
|
||||
text = message.get("text")
|
||||
if not isinstance(text, str) or len(text) > MAX_TEXT_CHARS:
|
||||
return
|
||||
room.pending_text = text
|
||||
self._schedule_save(room)
|
||||
|
||||
elif msg_type == "ping":
|
||||
await client.websocket.send_json({"type": "pong", "t": int(time.time() * 1000)})
|
||||
|
||||
# -- broadcasting -------------------------------------------------------
|
||||
async def _broadcast(self, room: CollabRoom, message: dict[str, Any], exclude: int | None = None) -> None:
|
||||
dead: list[CollabClient] = []
|
||||
for client in list(room.clients.values()):
|
||||
if exclude is not None and client.conn_id == exclude:
|
||||
continue
|
||||
try:
|
||||
await client.websocket.send_json(message)
|
||||
except Exception:
|
||||
dead.append(client)
|
||||
for client in dead:
|
||||
room.clients.pop(client.conn_id, None)
|
||||
|
||||
# -- persistence --------------------------------------------------------
|
||||
def _schedule_save(self, room: CollabRoom) -> None:
|
||||
if room.save_task and not room.save_task.done():
|
||||
room.save_task.cancel()
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError: # pragma: no cover - no running loop (tests)
|
||||
return
|
||||
room.save_task = loop.create_task(self._debounced_save(room))
|
||||
|
||||
async def _debounced_save(self, room: CollabRoom) -> None:
|
||||
try:
|
||||
await asyncio.sleep(self._save_debounce)
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
await self._flush(room)
|
||||
|
||||
async def _flush(self, room: CollabRoom) -> None:
|
||||
"""Write the last received text snapshot to disk (if any)."""
|
||||
async with room.lock:
|
||||
text = room.pending_text
|
||||
room.pending_text = None
|
||||
if text is None:
|
||||
return
|
||||
try:
|
||||
await asyncio.to_thread(room.file_path.write_text, text, encoding="utf-8")
|
||||
logger.debug("Collab persisted %s", room.key)
|
||||
except OSError as exc:
|
||||
logger.warning("Collab persist failed for %s: %s", room.key, exc)
|
||||
|
||||
|
||||
#: Process-wide singleton used by the WebSocket endpoint.
|
||||
collab_manager = CollabManager()
|
||||
@@ -11,14 +11,15 @@ Docker usage:
|
||||
docker exec obsigate python backend/create_admin.py list
|
||||
"""
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
# Add parent directory to path for imports
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from backend.auth.user_store import create_user, get_all_users, delete_user
|
||||
from backend.auth.user_store import create_user, delete_user, get_all_users
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -0,0 +1,431 @@
|
||||
"""
|
||||
Export utilities for ObsiGate — standalone HTML, Markdown bundle (.zip) and ePub.
|
||||
|
||||
Each function converts vault content (markdown notes) into a self-contained
|
||||
downloadable artifact. Rendering reuses ``mistune`` (already a core dependency)
|
||||
so no extra markdown engine is required. The ePub generator builds a minimal
|
||||
but valid EPUB 3 container by hand (``zipfile`` only) so there is no hard
|
||||
dependency on ``ebooklib``.
|
||||
|
||||
All functions raise :class:`ExportError` on any failure (missing file, binary
|
||||
content, unreadable source, ...) so callers can translate to HTTP errors.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import datetime
|
||||
import html as html_mod
|
||||
import io
|
||||
import logging
|
||||
import mimetypes
|
||||
import re
|
||||
import unicodedata
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import frontmatter
|
||||
import mistune
|
||||
|
||||
logger = logging.getLogger("obsigate.export")
|
||||
|
||||
|
||||
class ExportError(Exception):
|
||||
"""Raised when an export operation cannot be completed."""
|
||||
|
||||
|
||||
# Cached mistune renderer (singleton) — mirrors backend.main's configuration.
|
||||
_markdown = mistune.create_markdown(
|
||||
escape=False,
|
||||
plugins=["table", "strikethrough", "footnotes", "task_lists"],
|
||||
)
|
||||
|
||||
# File extensions considered "safe" text files for markdown-like exports.
|
||||
_MARKDOWN_EXTS = {".md", ".markdown", ".mdown", ".mkd", ".txt"}
|
||||
|
||||
_HTML_CSS = """
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
max-width: 760px;
|
||||
margin: 40px auto;
|
||||
padding: 0 24px;
|
||||
line-height: 1.7;
|
||||
color: #1a1a2e;
|
||||
font-size: 16px;
|
||||
}
|
||||
h1 { font-size: 28px; border-bottom: 2px solid #333; padding-bottom: 8px; margin-top: 0; }
|
||||
h2 { font-size: 22px; margin-top: 28px; border-bottom: 1px solid #ddd; padding-bottom: 4px; }
|
||||
h3 { font-size: 18px; margin-top: 24px; }
|
||||
h4, h5, h6 { font-size: 16px; margin-top: 20px; }
|
||||
p { margin: 10px 0; }
|
||||
pre {
|
||||
background: #f5f5f5;
|
||||
border: 1px solid #e0e0e0;
|
||||
border-radius: 6px;
|
||||
padding: 12px 16px;
|
||||
font-size: 14px;
|
||||
overflow-x: auto;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
code { font-size: 14px; background: #f5f5f5; padding: 2px 5px; border-radius: 3px; }
|
||||
pre code { background: none; padding: 0; }
|
||||
a { color: #4f46e5; text-decoration: none; }
|
||||
a:hover { text-decoration: underline; }
|
||||
img { max-width: 100%; border-radius: 4px; }
|
||||
blockquote { border-left: 3px solid #ccc; padding-left: 14px; color: #555; margin: 12px 0; }
|
||||
table { border-collapse: collapse; width: 100%; margin: 12px 0; }
|
||||
th, td { border: 1px solid #ddd; padding: 6px 10px; text-align: left; font-size: 14px; }
|
||||
th { background: #f0f0f0; font-weight: 600; }
|
||||
ul, ol { margin: 8px 0; padding-left: 24px; }
|
||||
li { margin: 2px 0; }
|
||||
hr { border: none; border-top: 1px solid #ddd; margin: 20px 0; }
|
||||
nav.export-nav {
|
||||
border: 1px solid #e0e0e0;
|
||||
border-radius: 8px;
|
||||
padding: 14px 18px;
|
||||
margin: 16px 0 28px;
|
||||
background: #fafafa;
|
||||
}
|
||||
nav.export-nav h2 { border: none; margin: 0 0 8px; font-size: 15px; text-transform: uppercase; letter-spacing: .04em; }
|
||||
nav.export-nav ul { margin: 0; padding-left: 20px; }
|
||||
.wikilink-missing { color: #c0392b; }
|
||||
"""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _slugify(text: str) -> str:
|
||||
"""URL-safe slug from arbitrary text (Unicode-aware)."""
|
||||
text = html_mod.unescape(re.sub(r"<[^>]+>", "", text))
|
||||
text = text.lower()
|
||||
text = unicodedata.normalize("NFD", text)
|
||||
text = "".join(ch for ch in text if not unicodedata.combining(ch))
|
||||
cleaned = []
|
||||
for ch in text:
|
||||
if unicodedata.category(ch).startswith(("L", "N")) or ch in (" ", "-"):
|
||||
cleaned.append(ch)
|
||||
text = re.sub(r"\s+", "-", "".join(cleaned))
|
||||
text = re.sub(r"-+", "-", text)
|
||||
return text.strip("-") or "document"
|
||||
|
||||
|
||||
def _resolve(vault_path: Path, file_path: Path | str) -> Path:
|
||||
"""Resolve *file_path* against *vault_path*, accepting absolute or relative.
|
||||
|
||||
Returns an absolute :class:`Path`. Does not require the file to exist.
|
||||
"""
|
||||
file_path = Path(file_path)
|
||||
if file_path.is_absolute():
|
||||
return file_path
|
||||
return (Path(vault_path) / file_path).resolve()
|
||||
|
||||
|
||||
def _read_text(path: Path) -> str:
|
||||
"""Read a text file, raising :class:`ExportError` on missing/binary content."""
|
||||
if not path.exists() or not path.is_file():
|
||||
raise ExportError(f"File not found: {path}")
|
||||
try:
|
||||
raw = path.read_bytes()
|
||||
except OSError as e:
|
||||
raise ExportError(f"Cannot read file {path}: {e}") from e
|
||||
if b"\x00" in raw[:4096]:
|
||||
raise ExportError(f"File appears to be binary: {path}")
|
||||
return raw.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def _strip_frontmatter(text: str) -> tuple[str, dict]:
|
||||
"""Split frontmatter from a markdown document.
|
||||
|
||||
Returns ``(body, metadata)``. Falls back gracefully when no frontmatter.
|
||||
"""
|
||||
try:
|
||||
post = frontmatter.loads(text)
|
||||
return post.content, dict(post.metadata or {})
|
||||
except Exception:
|
||||
return text, {}
|
||||
|
||||
|
||||
def _safe_name(name: str) -> str:
|
||||
"""ASCII-safe, filename-safe download name."""
|
||||
cleaned = "".join(
|
||||
c for c in name if c.isascii() and (c.isalnum() or c in " _-.")
|
||||
).strip()
|
||||
return cleaned or "document"
|
||||
|
||||
|
||||
def _collect_markdown_files(vault_path: Path) -> list[Path]:
|
||||
"""List all markdown files in the vault, sorted by relative path."""
|
||||
vault_path = Path(vault_path)
|
||||
results: list[Path] = []
|
||||
if not vault_path.is_dir():
|
||||
return results
|
||||
for p in sorted(vault_path.rglob("*")):
|
||||
if p.is_file() and p.suffix.lower() in _MARKDOWN_EXTS:
|
||||
results.append(p)
|
||||
return results
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Markdown → HTML (shared by HTML and ePub exporters)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _inline_images(md: str, file_dir: Path, vault_path: Path) -> str:
|
||||
"""Replace image references with base64 data URIs.
|
||||
|
||||
Supports standard markdown ```` and Obsidian embeds
|
||||
``![[image.png]]`` / ``![[image.png|300]]``. Sources are resolved relative
|
||||
to the note's directory, then the vault root.
|
||||
"""
|
||||
candidates = [file_dir, vault_path]
|
||||
|
||||
def _to_data_uri(src: str) -> str:
|
||||
src = src.strip().strip("<>")
|
||||
if src.startswith(("http://", "https://", "data:")):
|
||||
return src # leave remote/data URLs untouched
|
||||
src_path = None
|
||||
for base in candidates:
|
||||
candidate = (base / src).resolve()
|
||||
if candidate.is_file():
|
||||
src_path = candidate
|
||||
break
|
||||
if src_path is None:
|
||||
return src # unresolved — leave as-is
|
||||
try:
|
||||
data = src_path.read_bytes()
|
||||
mime = mimetypes.guess_type(str(src_path))[0] or "application/octet-stream"
|
||||
b64 = base64.b64encode(data).decode("ascii")
|
||||
return f"data:{mime};base64,{b64}"
|
||||
except OSError:
|
||||
return src
|
||||
|
||||
# Obsidian embeds: ![[path]] or ![[path|size]]
|
||||
def _embed(match):
|
||||
inner = match.group(1).strip()
|
||||
target = inner.split("|", 1)[0].strip()
|
||||
return f"})"
|
||||
|
||||
md = re.sub(r"!\[\[([^\]]+)\]\]", _embed, md)
|
||||
|
||||
# Standard markdown images
|
||||
def _img(match):
|
||||
alt = match.group(1)
|
||||
src = match.group(2)
|
||||
return f"})"
|
||||
|
||||
md = re.sub(r"!\[([^\]]*)\]\(([^)]+)\)", _img, md)
|
||||
return md
|
||||
|
||||
|
||||
def _convert_wikilinks(md: str, vault_path: Path, current: Path) -> str:
|
||||
"""Convert ``[[Note]]`` / ``[[Note|display]]`` to cross-file HTML links."""
|
||||
md_files = {p.stem.lower(): p for p in _collect_markdown_files(vault_path)}
|
||||
|
||||
def _replace(match):
|
||||
target = match.group(1).strip()
|
||||
display = (match.group(2) or target).strip()
|
||||
|
||||
# Same-document anchor
|
||||
if target.startswith("#"):
|
||||
return f'<a href="#{_slugify(target[1:])}">{html_mod.escape(display)}</a>'
|
||||
|
||||
# Resolve to another file — link to its exported .html sibling
|
||||
key = Path(target).stem.lower()
|
||||
if key in md_files:
|
||||
href = f"{_slugify(md_files[key].stem)}.html"
|
||||
return f'<a href="{href}">{html_mod.escape(display)}</a>'
|
||||
|
||||
return f'<span class="wikilink-missing">{html_mod.escape(display)}</span>'
|
||||
|
||||
return re.sub(r"\[\[([^\]|]+)(?:\|([^\]]+))?\]\]", _replace, md)
|
||||
|
||||
|
||||
def _render_body(md: str, file_dir: Path, vault_path: Path, current: Path) -> str:
|
||||
"""Render raw markdown to an HTML fragment (images inlined, wikilinks resolved)."""
|
||||
md = _inline_images(md, file_dir, vault_path)
|
||||
md = _convert_wikilinks(md, vault_path, current)
|
||||
return _markdown(md)
|
||||
|
||||
|
||||
def _build_nav(vault_path: Path, current: Path) -> str:
|
||||
"""Build a navigation block linking to every other markdown note in the vault."""
|
||||
items = []
|
||||
for p in _collect_markdown_files(vault_path):
|
||||
if p.resolve() == Path(current).resolve():
|
||||
continue
|
||||
stem = p.stem
|
||||
items.append(f'<li><a href="{_slugify(stem)}.html">{html_mod.escape(stem)}</a></li>')
|
||||
if not items:
|
||||
return ""
|
||||
return (
|
||||
'<nav class="export-nav"><h2>Notes</h2><ul>'
|
||||
+ "".join(items)
|
||||
+ "</ul></nav>"
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public exporters
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def export_html(vault_path: Path, file_path: Path) -> bytes:
|
||||
"""Convert a markdown note into a standalone HTML document.
|
||||
|
||||
Images are inlined as base64 data URIs, wikilinks become cross-file links,
|
||||
and a navigation block lists every other note in the vault.
|
||||
|
||||
Returns:
|
||||
UTF-8 encoded HTML as bytes.
|
||||
"""
|
||||
vault_path = Path(vault_path)
|
||||
path = _resolve(vault_path, file_path)
|
||||
raw = _read_text(path)
|
||||
body, metadata = _strip_frontmatter(raw)
|
||||
title = str(metadata.get("title") or path.stem)
|
||||
|
||||
rendered = _render_body(body, path.parent, vault_path, path)
|
||||
nav = _build_nav(vault_path, path)
|
||||
|
||||
html_doc = f"""<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="generator" content="ObsiGate">
|
||||
<title>{html_mod.escape(title)}</title>
|
||||
<style>{_HTML_CSS}</style>
|
||||
</head>
|
||||
<body>
|
||||
<header><h1>{html_mod.escape(title)}</h1></header>
|
||||
{nav}
|
||||
{rendered}
|
||||
</body>
|
||||
</html>"""
|
||||
return html_doc.encode("utf-8")
|
||||
|
||||
|
||||
def export_md_bundle(vault_path: Path, directory_path: Path) -> bytes:
|
||||
"""Zip a directory (or single file) of markdown, preserving structure.
|
||||
|
||||
Returns:
|
||||
ZIP archive as bytes.
|
||||
"""
|
||||
vault_path = Path(vault_path)
|
||||
path = _resolve(vault_path, directory_path)
|
||||
|
||||
if not path.exists():
|
||||
raise ExportError(f"Path not found: {path}")
|
||||
|
||||
buffer = io.BytesIO()
|
||||
try:
|
||||
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
if path.is_dir():
|
||||
base = path
|
||||
for p in sorted(path.rglob("*")):
|
||||
if p.is_file():
|
||||
arcname = p.relative_to(base).as_posix()
|
||||
zf.write(p, arcname=arcname)
|
||||
# Avoid emitting an empty (invalid) zip for an empty directory
|
||||
if not any(p.is_file() for p in path.rglob("*")):
|
||||
zf.writestr(".empty", "")
|
||||
else:
|
||||
zf.write(path, arcname=path.name)
|
||||
except OSError as e:
|
||||
raise ExportError(f"Cannot create bundle: {e}") from e
|
||||
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def export_epub(vault_path: Path, file_path: Path) -> bytes:
|
||||
"""Convert a markdown note into a minimal, valid EPUB 3 container.
|
||||
|
||||
Uses only ``zipfile`` + ``mistune`` (no ``ebooklib`` dependency). Images
|
||||
are inlined as base64 so the ePub is fully self-contained.
|
||||
|
||||
Returns:
|
||||
EPUB archive as bytes.
|
||||
"""
|
||||
vault_path = Path(vault_path)
|
||||
path = _resolve(vault_path, file_path)
|
||||
raw = _read_text(path)
|
||||
body, metadata = _strip_frontmatter(raw)
|
||||
title = str(metadata.get("title") or path.stem)
|
||||
author = str(metadata.get("author") or "ObsiGate")
|
||||
|
||||
rendered = _render_body(body, path.parent, vault_path, path)
|
||||
|
||||
uid = f"obsigate-{_slugify(title)}-{int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())}"
|
||||
xhtml = f"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
|
||||
<head>
|
||||
<title>{html_mod.escape(title)}</title>
|
||||
<style>{_HTML_CSS}</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{html_mod.escape(title)}</h1>
|
||||
{rendered}
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
# EPUB requires a valid XHTML-ish body; strip our HTML5-only doctype is not
|
||||
# needed since we emit XML directly above. Escape any bare ampersands in
|
||||
# text nodes outside tags is delegated to mistune's own escaping.
|
||||
content_opf = f"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="bookid">
|
||||
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||
<dc:identifier id="bookid">{html_mod.escape(uid)}</dc:identifier>
|
||||
<dc:title>{html_mod.escape(title)}</dc:title>
|
||||
<dc:creator>{html_mod.escape(author)}</dc:creator>
|
||||
<dc:language>fr</dc:language>
|
||||
<meta property="dcterms:modified">{datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')}</meta>
|
||||
</metadata>
|
||||
<manifest>
|
||||
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
|
||||
<item id="chapter1" href="chapter1.xhtml" media-type="application/xhtml+xml"/>
|
||||
</manifest>
|
||||
<spine toc="ncx">
|
||||
<itemref idref="chapter1"/>
|
||||
</spine>
|
||||
</package>"""
|
||||
|
||||
toc_ncx = f"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
|
||||
<head>
|
||||
<meta name="dtb:uid" content="{html_mod.escape(uid)}"/>
|
||||
</head>
|
||||
<docTitle><text>{html_mod.escape(title)}</text></docTitle>
|
||||
<navMap>
|
||||
<navPoint id="navpoint-1" playOrder="1">
|
||||
<navLabel><text>{html_mod.escape(title)}</text></navLabel>
|
||||
<content src="chapter1.xhtml"/>
|
||||
</navPoint>
|
||||
</navMap>
|
||||
</ncx>"""
|
||||
|
||||
container_xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
||||
<rootfiles>
|
||||
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
|
||||
</rootfiles>
|
||||
</container>"""
|
||||
|
||||
buffer = io.BytesIO()
|
||||
try:
|
||||
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
# `mimetype` must be the first entry, stored (uncompressed).
|
||||
zf.writestr(
|
||||
zipfile.ZipInfo("mimetype"),
|
||||
"application/epub+zip",
|
||||
compress_type=zipfile.ZIP_STORED,
|
||||
)
|
||||
zf.writestr("META-INF/container.xml", container_xml)
|
||||
zf.writestr("OEBPS/content.opf", content_opf)
|
||||
zf.writestr("OEBPS/toc.ncx", toc_ncx)
|
||||
zf.writestr("OEBPS/chapter1.xhtml", xhtml)
|
||||
except OSError as e:
|
||||
raise ExportError(f"Cannot create ePub: {e}") from e
|
||||
|
||||
return buffer.getvalue()
|
||||
+49
-6
@@ -1,10 +1,10 @@
|
||||
# backend/history.py
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
import shutil
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.history")
|
||||
|
||||
@@ -18,7 +18,7 @@ def _get_bookmarks_file(username: str) -> Path:
|
||||
HISTORY_DIR.mkdir(parents=True, exist_ok=True)
|
||||
return HISTORY_DIR / f"{username}_bookmarks.json"
|
||||
|
||||
def _read_data(file: Path) -> List[Dict[str, Any]]:
|
||||
def _read_data(file: Path) -> list[dict[str, Any]]:
|
||||
if not file.exists():
|
||||
return []
|
||||
try:
|
||||
@@ -27,7 +27,7 @@ def _read_data(file: Path) -> List[Dict[str, Any]]:
|
||||
logger.error(f"Failed to read data from {file.name}: {e}")
|
||||
return []
|
||||
|
||||
def _write_data(file: Path, data: List[Dict[str, Any]]):
|
||||
def _write_data(file: Path, data: list[dict[str, Any]]):
|
||||
try:
|
||||
tmp = file.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
|
||||
@@ -58,7 +58,7 @@ def record_open(username: str, vault: str, path: str, title: str = ""):
|
||||
|
||||
_write_data(_get_history_file(username), history)
|
||||
|
||||
def get_recent_opened(username: str, vault_filter: Optional[str] = None, limit: int = 20) -> List[Dict[str, Any]]:
|
||||
def get_recent_opened(username: str, vault_filter: str | None = None, limit: int = 20) -> list[dict[str, Any]]:
|
||||
"""Get the most recently opened files for a user."""
|
||||
if not username:
|
||||
return []
|
||||
@@ -105,7 +105,7 @@ def toggle_bookmark(username: str, vault: str, path: str, title: str = ""):
|
||||
_write_data(b_file, bookmarks)
|
||||
return True
|
||||
|
||||
def get_bookmarks(username: str, vault_filter: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
def get_bookmarks(username: str, vault_filter: str | None = None) -> list[dict[str, Any]]:
|
||||
"""Get the bookmarks for a user."""
|
||||
if not username:
|
||||
return []
|
||||
@@ -123,3 +123,46 @@ def is_bookmarked(username: str, vault: str, path: str) -> bool:
|
||||
return False
|
||||
bookmarks = _read_data(_get_bookmarks_file(username))
|
||||
return any(b["vault"] == vault and b["path"] == path for b in bookmarks)
|
||||
|
||||
|
||||
def update_bookmarks_after_rename(vault: str, old_path: str, new_path: str):
|
||||
"""Update all users' bookmarks when a file is renamed."""
|
||||
if not HISTORY_DIR.exists():
|
||||
return
|
||||
for bfile in HISTORY_DIR.glob("*_bookmarks.json"):
|
||||
try:
|
||||
bookmarks = _read_data(bfile)
|
||||
updated = False
|
||||
for b in bookmarks:
|
||||
if b.get("vault") == vault and b.get("path") == old_path:
|
||||
b["path"] = new_path
|
||||
# Update title from new path
|
||||
new_title = new_path.split("/")[-1].replace(".md", "").replace("-", " ").replace("_", " ")
|
||||
b["title"] = new_title
|
||||
updated = True
|
||||
if updated:
|
||||
_write_data(bfile, bookmarks)
|
||||
logger.info(f"Updated bookmarks in {bfile.name}: {vault}/{old_path} -> {new_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to update bookmarks in {bfile.name}: {e}")
|
||||
|
||||
|
||||
def update_history_after_rename(vault: str, old_path: str, new_path: str):
|
||||
"""Update all users' history entries when a file is renamed."""
|
||||
if not HISTORY_DIR.exists():
|
||||
return
|
||||
for hfile in HISTORY_DIR.glob("*.json"):
|
||||
if "_bookmarks" in hfile.name:
|
||||
continue
|
||||
try:
|
||||
history = _read_data(hfile)
|
||||
updated = False
|
||||
for h in history:
|
||||
if h.get("vault") == vault and h.get("path") == old_path:
|
||||
h["path"] = new_path
|
||||
updated = True
|
||||
if updated:
|
||||
_write_data(hfile, history)
|
||||
logger.info(f"Updated history in {hfile.name}: {vault}/{old_path} -> {new_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to update history in {hfile.name}: {e}")
|
||||
|
||||
+10
-11
@@ -1,8 +1,7 @@
|
||||
import re
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import re
|
||||
from html import escape as html_escape
|
||||
from pathlib import Path
|
||||
|
||||
from backend.attachment_indexer import resolve_image_path
|
||||
|
||||
@@ -13,8 +12,8 @@ def preprocess_images(
|
||||
content: str,
|
||||
vault_name: str,
|
||||
vault_root: Path,
|
||||
current_file_path: Optional[Path] = None,
|
||||
attachments_path: Optional[str] = None
|
||||
current_file_path: Path | None = None,
|
||||
attachments_path: str | None = None
|
||||
) -> str:
|
||||
"""Preprocess markdown content to transform all Obsidian image syntaxes.
|
||||
|
||||
@@ -55,8 +54,8 @@ def _process_html_img_in_link(
|
||||
content: str,
|
||||
vault_name: str,
|
||||
vault_root: Path,
|
||||
current_file_path: Optional[Path],
|
||||
attachments_path: Optional[str]
|
||||
current_file_path: Path | None,
|
||||
attachments_path: str | None
|
||||
) -> str:
|
||||
"""Process [<img ... src="path"/>](url) syntax.
|
||||
|
||||
@@ -119,8 +118,8 @@ def _process_wikilink_embeds(
|
||||
content: str,
|
||||
vault_name: str,
|
||||
vault_root: Path,
|
||||
current_file_path: Optional[Path],
|
||||
attachments_path: Optional[str]
|
||||
current_file_path: Path | None,
|
||||
attachments_path: str | None
|
||||
) -> str:
|
||||
"""Process ![[image]] and ![[path/to/image]] wiki-link embeds.
|
||||
|
||||
@@ -158,8 +157,8 @@ def _process_standard_images(
|
||||
content: str,
|
||||
vault_name: str,
|
||||
vault_root: Path,
|
||||
current_file_path: Optional[Path],
|
||||
attachments_path: Optional[str]
|
||||
current_file_path: Path | None,
|
||||
attachments_path: str | None
|
||||
) -> str:
|
||||
"""Process  standard markdown images.
|
||||
|
||||
|
||||
+337
-112
@@ -1,21 +1,23 @@
|
||||
import os
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from collections.abc import Callable
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import frontmatter
|
||||
|
||||
logger = logging.getLogger("obsigate.indexer")
|
||||
|
||||
# Global in-memory index
|
||||
index: Dict[str, Dict[str, Any]] = {}
|
||||
index: dict[str, dict[str, Any]] = {}
|
||||
|
||||
# Vault config: {name: {path, attachmentsPath, scanAttachmentsOnStartup}}
|
||||
vault_config: Dict[str, Dict[str, Any]] = {}
|
||||
vault_config: dict[str, dict[str, Any]] = {}
|
||||
|
||||
# Thread-safe lock for index updates
|
||||
_index_lock = threading.Lock()
|
||||
@@ -27,6 +29,9 @@ _async_index_lock: asyncio.Lock | None = None # initialized lazily
|
||||
# (e.g. the inverted index in search.py) can detect staleness.
|
||||
_index_generation: int = 0
|
||||
|
||||
# Timestamp of last full index rebuild (ISO format, empty if never built)
|
||||
_last_full_index_ts: str = ""
|
||||
|
||||
# Hook for incremental inverted index updates: called as (action, vault, path, file_info)
|
||||
_on_index_change: Callable[..., None] | None = None
|
||||
|
||||
@@ -41,13 +46,13 @@ def set_index_change_hook(hook):
|
||||
_on_index_change = hook
|
||||
|
||||
# O(1) lookup table for wikilink resolution: {filename_lower: [{vault, path}, ...]}
|
||||
_file_lookup: Dict[str, List[Dict[str, str]]] = {}
|
||||
_file_lookup: dict[str, list[dict[str, str]]] = {}
|
||||
|
||||
# Backlink index: {vault_name: {relative_path: [{vault, path, title}, ...]}}
|
||||
_backlink_index: Dict[str, Dict[str, List[Dict[str, str]]]] = {}
|
||||
_backlink_index: dict[str, dict[str, list[dict[str, str]]]] = {}
|
||||
|
||||
# O(1) path index for tree filtering: {vault_name: [{path, name, type}, ...]}
|
||||
path_index: Dict[str, List[Dict[str, str]]] = {}
|
||||
path_index: dict[str, list[dict[str, str]]] = {}
|
||||
|
||||
# Maximum content size stored per file for in-memory search (bytes)
|
||||
SEARCH_CONTENT_LIMIT = 100_000
|
||||
@@ -57,21 +62,23 @@ SUPPORTED_EXTENSIONS = {
|
||||
".md", ".txt", ".log", ".py", ".js", ".ts", ".jsx", ".tsx",
|
||||
".sh", ".bash", ".zsh", ".fish", ".bat", ".cmd", ".ps1",
|
||||
".json", ".yaml", ".yml", ".toml", ".xml", ".csv",
|
||||
".cfg", ".ini", ".conf", ".env",
|
||||
".cfg", ".ini", ".conf", ".env", ".pdf",
|
||||
".html", ".css", ".scss", ".less",
|
||||
".java", ".c", ".cpp", ".h", ".hpp", ".cs", ".go", ".rs", ".rb",
|
||||
".php", ".sql", ".r", ".m", ".swift", ".kt",
|
||||
".dockerfile", ".makefile", ".cmake",
|
||||
".excalidraw",
|
||||
".excalidraw.md",
|
||||
}
|
||||
|
||||
|
||||
# Ignored directories (configurable via OBSIGATE_IGNORED_DIRS env var)
|
||||
_DEFAULT_IGNORED = {'.obsidian', '.trash', '.git', '__pycache__', 'node_modules', '.obsigate-backup'}
|
||||
_env_ignored = os.environ.get("OBSIGATE_IGNORED_DIRS", "")
|
||||
IGNORED_DIRS = set(d.strip() for d in _env_ignored.split(",") if d.strip()) if _env_ignored else _DEFAULT_IGNORED.copy()
|
||||
IGNORED_DIRS = {d.strip() for d in _env_ignored.split(",") if d.strip()} if _env_ignored else _DEFAULT_IGNORED.copy()
|
||||
|
||||
|
||||
def load_vault_config() -> Dict[str, Dict[str, Any]]:
|
||||
def load_vault_config() -> dict[str, dict[str, Any]]:
|
||||
"""Read VAULT_N_* and DIR_N_* env vars and return vault configuration.
|
||||
|
||||
Scans environment variables ``VAULT_1_NAME``/``VAULT_1_PATH``,
|
||||
@@ -89,7 +96,7 @@ def load_vault_config() -> Dict[str, Dict[str, Any]]:
|
||||
- scanAttachmentsOnStartup: boolean (default True)
|
||||
- type: "VAULT" or "DIR"
|
||||
"""
|
||||
vaults: Dict[str, Dict[str, Any]] = {}
|
||||
vaults: dict[str, dict[str, Any]] = {}
|
||||
n = 1
|
||||
while True:
|
||||
name = os.environ.get(f"VAULT_{n}_NAME")
|
||||
@@ -135,7 +142,7 @@ _CODE_BLOCK_RE = re.compile(r'```[\s\S]*?```', re.MULTILINE)
|
||||
_INLINE_CODE_RE = re.compile(r'`[^`]+`')
|
||||
|
||||
|
||||
def _extract_tags(post: frontmatter.Post) -> List[str]:
|
||||
def _extract_tags(post: frontmatter.Post) -> list[str]:
|
||||
"""Extract tags from frontmatter metadata.
|
||||
|
||||
|
||||
@@ -158,7 +165,7 @@ def _extract_tags(post: frontmatter.Post) -> List[str]:
|
||||
return tags
|
||||
|
||||
|
||||
def _extract_inline_tags(content: str) -> List[str]:
|
||||
def _extract_inline_tags(content: str) -> list[str]:
|
||||
"""Extract inline #tag patterns from markdown content.
|
||||
|
||||
Strips fenced and inline code blocks before scanning to avoid
|
||||
@@ -194,6 +201,178 @@ def _extract_title(post: frontmatter.Post, filepath: Path) -> str:
|
||||
return str(title)
|
||||
|
||||
|
||||
_EXCALIDRAW_TEXT_FIELDS = ("text", "originalText", "label", "title")
|
||||
|
||||
|
||||
def extract_excalidraw_text_from_elements(elements: list[dict[str, Any]]) -> str:
|
||||
"""Concatenate all user-visible text from an Excalidraw elements list.
|
||||
|
||||
Iterates diagram elements and collects the text-bearing fields
|
||||
(``text`` for text elements, ``label``/``title`` for bound shapes,
|
||||
``originalText`` as the stable source of a text element). Non-text
|
||||
elements and geometry-only shapes contribute nothing. This gives the
|
||||
TF-IDF search engine human-readable content instead of raw JSON.
|
||||
"""
|
||||
chunks: list[str] = []
|
||||
for el in elements:
|
||||
if not isinstance(el, dict):
|
||||
continue
|
||||
collected = set()
|
||||
for field in _EXCALIDRAW_TEXT_FIELDS:
|
||||
val = el.get(field)
|
||||
if isinstance(val, str) and val.strip():
|
||||
collected.add(val.strip())
|
||||
if collected:
|
||||
chunks.append(" ".join(sorted(collected)))
|
||||
return "\n".join(chunks)
|
||||
|
||||
|
||||
_EXCALIDRAW_B64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
|
||||
|
||||
|
||||
def _decompress_excalidraw(compressed: str) -> dict[str, Any] | None:
|
||||
"""Decompress the Obsidian ``compressed-json`` block of a .excalidraw.md file.
|
||||
|
||||
The Obsidian Excalidraw plugin stores scene data as an ``lz-string``
|
||||
``compressToBase64`` payload (LZ-based compression, alphabet = standard
|
||||
base64). We port the reference ``lz-string`` ``_decompress`` algorithm
|
||||
(bitsPerChar=6/base64 key, resetValue=32) in pure Python so indexation
|
||||
can recover the diagram text without the JS client. The port is validated
|
||||
against multiple JS-truth fixtures in ``test_excalidraw.py`` including
|
||||
accented text and edge cases. Returns the parsed scene dict, or None if
|
||||
the payload is not valid base64-lz or does not parse as JSON.
|
||||
|
||||
Only ``decompress`` is needed for indexation (read side); compression
|
||||
stays on the JS client.
|
||||
"""
|
||||
if not compressed:
|
||||
return None
|
||||
|
||||
length = len(compressed)
|
||||
|
||||
def _get_char_value(index: int) -> int:
|
||||
# Mirror JS: input.charAt(index), 0-indexed.
|
||||
ch = compressed[index] if 0 <= index < length else "="
|
||||
pos = _EXCALIDRAW_B64_ALPHABET.find(ch)
|
||||
return 0 if pos == -1 else pos
|
||||
|
||||
# ── Build a bit reader over the base64 characters ────────────────────
|
||||
# JS decompressFromBase64 calls _decompress(length, 32, getNextValue).
|
||||
# state = current 6-bit value + position mask + next char index.
|
||||
value = _get_char_value(0)
|
||||
position = 32 # resetValue
|
||||
index = 1
|
||||
|
||||
def _read_bit() -> int:
|
||||
nonlocal value, position, index
|
||||
resb = value & position
|
||||
position >>= 1
|
||||
if position == 0:
|
||||
position = 32
|
||||
value = _get_char_value(index)
|
||||
index += 1
|
||||
return 1 if resb > 0 else 0
|
||||
|
||||
def _read_bits(n: int) -> int:
|
||||
out = 0
|
||||
for i in range(n):
|
||||
out |= _read_bit() << i
|
||||
return out
|
||||
|
||||
# ── Decompressor state ────────────────────────────────────────────────
|
||||
dictionary: list[Any] = [0, 1, 2] # values 0,1,2 as in JS
|
||||
enlarge_in = 4
|
||||
dict_size = 4
|
||||
num_bits = 3
|
||||
|
||||
first = _read_bits(2)
|
||||
if first == 0:
|
||||
c = chr(_read_bits(8))
|
||||
elif first == 1:
|
||||
c = chr(_read_bits(16))
|
||||
elif first == 2:
|
||||
return None # empty stream
|
||||
else:
|
||||
return None
|
||||
|
||||
dictionary.append(c)
|
||||
w: str = c
|
||||
result = [c]
|
||||
|
||||
while True:
|
||||
if index > length:
|
||||
return None
|
||||
|
||||
code = _read_bits(num_bits)
|
||||
if code == 0:
|
||||
dictionary.append(chr(_read_bits(8)))
|
||||
dict_size += 1
|
||||
code = dict_size - 1
|
||||
enlarge_in -= 1
|
||||
elif code == 1:
|
||||
dictionary.append(chr(_read_bits(16)))
|
||||
dict_size += 1
|
||||
code = dict_size - 1
|
||||
enlarge_in -= 1
|
||||
elif code == 2:
|
||||
break # end of stream
|
||||
|
||||
if enlarge_in == 0:
|
||||
enlarge_in = 1 << num_bits
|
||||
num_bits += 1
|
||||
|
||||
if code < len(dictionary) and dictionary[code]:
|
||||
entry = dictionary[code]
|
||||
elif code == dict_size:
|
||||
entry = w + w[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
result.append(entry)
|
||||
dictionary.append(w + entry[0])
|
||||
dict_size += 1
|
||||
enlarge_in -= 1
|
||||
|
||||
w = entry
|
||||
if enlarge_in == 0:
|
||||
enlarge_in = 1 << num_bits
|
||||
num_bits += 1
|
||||
|
||||
text = "".join(result)
|
||||
try:
|
||||
data = json.loads(text)
|
||||
except Exception:
|
||||
return None
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
return data
|
||||
|
||||
|
||||
def extract_excalidraw_indexable(raw: str) -> str:
|
||||
"""Return indexable text content for a raw .excalidraw / .excalidraw.md file.
|
||||
|
||||
Supports both the pure JSON format (``type: "excalidraw"``) and the
|
||||
Obsidian ``compressed-json`` block. Falls back to ``""`` when neither
|
||||
format can be parsed so the file is still indexed (title only).
|
||||
"""
|
||||
# .excalidraw.md — Obsidian plugin embeds a compressed-json block
|
||||
if "excalidraw-plugin:" in raw:
|
||||
m = re.search(r"```compressed-json\n(.*?)\n```", raw, re.DOTALL)
|
||||
if m:
|
||||
parsed = _decompress_excalidraw(m.group(1).strip())
|
||||
if parsed:
|
||||
return extract_excalidraw_text_from_elements(parsed.get("elements") or [])
|
||||
return ""
|
||||
# Pure .excalidraw JSON
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except Exception:
|
||||
return ""
|
||||
if not isinstance(data, dict) or data.get("type") != "excalidraw":
|
||||
return ""
|
||||
return extract_excalidraw_text_from_elements(data.get("elements") or [])
|
||||
|
||||
|
||||
def parse_markdown_file(raw: str) -> frontmatter.Post:
|
||||
"""Parse markdown frontmatter, falling back to plain content if YAML is invalid.
|
||||
|
||||
@@ -215,10 +394,10 @@ def parse_markdown_file(raw: str) -> frontmatter.Post:
|
||||
match = re.match(r"^---\s*\r?\n.*?\r?\n---\s*\r?\n?", raw, flags=re.DOTALL)
|
||||
if match:
|
||||
content = raw[match.end():]
|
||||
return frontmatter.Post(content, **{})
|
||||
return frontmatter.Post(content)
|
||||
|
||||
|
||||
def _scan_vault(vault_name: str, vault_path: str, vault_cfg: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
def _scan_vault(vault_name: str, vault_path: str, vault_cfg: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
"""Synchronously scan a single vault directory and build file index.
|
||||
|
||||
Walks the vault tree, reads supported files, extracts metadata
|
||||
@@ -236,92 +415,121 @@ def _scan_vault(vault_name: str, vault_path: str, vault_cfg: Optional[Dict[str,
|
||||
Dict with keys ``files`` (list), ``tags`` (counter dict), ``path`` (str), ``paths`` (list).
|
||||
"""
|
||||
vault_root = Path(vault_path)
|
||||
files: List[Dict[str, Any]] = []
|
||||
tag_counts: Dict[str, int] = {}
|
||||
paths: List[Dict[str, str]] = []
|
||||
files: list[dict[str, Any]] = []
|
||||
tag_counts: dict[str, int] = {}
|
||||
paths: list[dict[str, str]] = []
|
||||
|
||||
if not vault_root.exists():
|
||||
logger.warning(f"Vault path does not exist: {vault_path}")
|
||||
return {"files": [], "tags": {}, "path": vault_path, "paths": []}
|
||||
|
||||
for fpath in vault_root.rglob("*"):
|
||||
# Skip ignored directories
|
||||
if any(part in IGNORED_DIRS for part in fpath.relative_to(vault_root).parts):
|
||||
continue
|
||||
root_resolved = vault_root.resolve(strict=False)
|
||||
|
||||
rel_path_str = str(fpath.relative_to(vault_root)).replace("\\", "/")
|
||||
|
||||
# Add all paths (files and directories) to path index
|
||||
if fpath.is_dir():
|
||||
# BUG-032: walk without following symlinks and refuse any symlink that
|
||||
# escapes the vault root, so external data can never be indexed/exposed.
|
||||
for dirpath, dirnames, filenames in os.walk(vault_root, followlinks=False):
|
||||
current_dir = Path(dirpath)
|
||||
|
||||
# Prune ignored and symlinked directories in place (no recursion).
|
||||
dirnames[:] = [
|
||||
d for d in dirnames
|
||||
if d not in IGNORED_DIRS and not (current_dir / d).is_symlink()
|
||||
]
|
||||
|
||||
for d in dirnames:
|
||||
dpath = current_dir / d
|
||||
rel_path_str = str(dpath.relative_to(vault_root)).replace("\\", "/")
|
||||
paths.append({
|
||||
"path": rel_path_str,
|
||||
"name": fpath.name,
|
||||
"name": d,
|
||||
"type": "directory"
|
||||
})
|
||||
continue
|
||||
|
||||
# Files only from here
|
||||
if not fpath.is_file():
|
||||
continue
|
||||
ext = fpath.suffix.lower()
|
||||
# Also match extensionless files named like Dockerfile, Makefile
|
||||
basename_lower = fpath.name.lower()
|
||||
if ext not in SUPPORTED_EXTENSIONS and basename_lower not in ("dockerfile", "makefile", "cmakelists.txt"):
|
||||
continue
|
||||
|
||||
# Add file to path index
|
||||
paths.append({
|
||||
"path": rel_path_str,
|
||||
"name": fpath.name,
|
||||
"type": "file"
|
||||
})
|
||||
|
||||
try:
|
||||
relative = fpath.relative_to(vault_root)
|
||||
stat = fpath.stat()
|
||||
modified = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat()
|
||||
|
||||
raw = fpath.read_text(encoding="utf-8", errors="replace")
|
||||
for fname in filenames:
|
||||
fpath = current_dir / fname
|
||||
|
||||
tags: List[str] = []
|
||||
title = fpath.stem.replace("-", " ").replace("_", " ")
|
||||
content_preview = raw[:200].strip()
|
||||
if fpath.is_symlink():
|
||||
try:
|
||||
target = fpath.resolve(strict=True)
|
||||
except OSError:
|
||||
continue
|
||||
try:
|
||||
target.relative_to(root_resolved)
|
||||
except ValueError:
|
||||
logger.warning(f"Skipping symlink outside vault: {fpath}")
|
||||
continue
|
||||
|
||||
if ext == ".md":
|
||||
post = parse_markdown_file(raw)
|
||||
tags = _extract_tags(post)
|
||||
# Merge inline #tags found in content body
|
||||
inline_tags = _extract_inline_tags(post.content)
|
||||
tags = list(set(tags) | set(inline_tags))
|
||||
title = _extract_title(post, fpath)
|
||||
content_preview = post.content[:200].strip()
|
||||
rel_path_str = str(fpath.relative_to(vault_root)).replace("\\", "/")
|
||||
|
||||
# Extract wikilinks for backlink index
|
||||
_extract_wikilinks_for_backlinks(
|
||||
vault_name, str(relative).replace("\\", "/"),
|
||||
title, post.content
|
||||
)
|
||||
ext = fpath.suffix.lower()
|
||||
# Also match extensionless files named like Dockerfile, Makefile
|
||||
basename_lower = fpath.name.lower()
|
||||
if ext not in SUPPORTED_EXTENSIONS and basename_lower not in ("dockerfile", "makefile", "cmakelists.txt"):
|
||||
continue
|
||||
|
||||
files.append({
|
||||
"path": str(relative).replace("\\", "/"),
|
||||
"title": title,
|
||||
"tags": tags,
|
||||
"content_preview": content_preview,
|
||||
"content": raw[:SEARCH_CONTENT_LIMIT],
|
||||
"size": stat.st_size,
|
||||
"modified": modified,
|
||||
"extension": ext,
|
||||
# Add file to path index
|
||||
paths.append({
|
||||
"path": rel_path_str,
|
||||
"name": fname,
|
||||
"type": "file"
|
||||
})
|
||||
|
||||
for tag in tags:
|
||||
tag_counts[tag] = tag_counts.get(tag, 0) + 1
|
||||
try:
|
||||
relative = fpath.relative_to(vault_root)
|
||||
stat = fpath.stat()
|
||||
modified = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat()
|
||||
|
||||
except PermissionError:
|
||||
logger.debug(f"Permission denied, skipping {fpath}")
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.error(f"Error indexing {fpath}: {e}")
|
||||
continue
|
||||
# PDF handling — special path (binary, uses pdf_reader)
|
||||
tags: list[str] = []
|
||||
if ext == ".pdf":
|
||||
from backend.pdf_reader import extract_pdf_metadata, extract_pdf_text
|
||||
raw = extract_pdf_text(fpath, max_chars=100000)
|
||||
pdf_meta = extract_pdf_metadata(fpath)
|
||||
title = pdf_meta.get("title") or fpath.stem.replace("-", " ").replace("_", " ")
|
||||
content_preview = raw[:200].strip()
|
||||
elif ext == ".excalidraw" or fpath.name.lower().endswith(".excalidraw.md"):
|
||||
raw = fpath.read_text(encoding="utf-8", errors="replace")
|
||||
raw = extract_excalidraw_indexable(raw)
|
||||
title = fpath.stem.replace(".excalidraw", "").replace("-", " ").replace("_", " ")
|
||||
content_preview = raw[:200].strip()
|
||||
else:
|
||||
raw = fpath.read_text(encoding="utf-8", errors="replace")
|
||||
title = fpath.stem.replace("-", " ").replace("_", " ")
|
||||
content_preview = raw[:200].strip()
|
||||
|
||||
if ext == ".md":
|
||||
post = parse_markdown_file(raw)
|
||||
tags = _extract_tags(post)
|
||||
inline_tags = _extract_inline_tags(post.content)
|
||||
tags = list(set(tags) | set(inline_tags))
|
||||
title = _extract_title(post, fpath)
|
||||
content_preview = post.content[:200].strip()
|
||||
|
||||
_extract_wikilinks_for_backlinks(
|
||||
vault_name, str(relative).replace("\\", "/"),
|
||||
title, post.content
|
||||
)
|
||||
|
||||
files.append({
|
||||
"path": str(relative).replace("\\", "/"),
|
||||
"title": title,
|
||||
"tags": tags,
|
||||
"content_preview": content_preview,
|
||||
"content": raw[:SEARCH_CONTENT_LIMIT],
|
||||
"size": stat.st_size,
|
||||
"modified": modified,
|
||||
"extension": ext,
|
||||
})
|
||||
|
||||
for tag in tags:
|
||||
tag_counts[tag] = tag_counts.get(tag, 0) + 1
|
||||
|
||||
except PermissionError:
|
||||
logger.debug(f"Permission denied, skipping {fpath}")
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.error(f"Error indexing {fpath}: {e}")
|
||||
continue
|
||||
|
||||
logger.info(f"Vault '{vault_name}': indexed {len(files)} files, {len(paths)} paths, {len(tag_counts)} unique tags")
|
||||
return {"files": files, "tags": tag_counts, "path": vault_path, "paths": paths, "config": {}}
|
||||
@@ -359,13 +567,13 @@ async def build_index(progress_callback=None) -> None:
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
async def _process_vault(name: str, config: Dict[str, Any]):
|
||||
async def _process_vault(name: str, config: dict[str, Any]):
|
||||
vault_path = config["path"]
|
||||
vault_data = await loop.run_in_executor(None, _scan_vault, name, vault_path, config)
|
||||
vault_data["config"] = config
|
||||
|
||||
# Build lookup entries for the new vault
|
||||
new_lookup_entries: Dict[str, List[Dict[str, str]]] = {}
|
||||
new_lookup_entries: dict[str, list[dict[str, str]]] = {}
|
||||
for f in vault_data["files"]:
|
||||
entry = {"vault": name, "path": f["path"]}
|
||||
fname = f["path"].rsplit("/", 1)[-1].lower()
|
||||
@@ -402,6 +610,10 @@ async def build_index(progress_callback=None) -> None:
|
||||
if tasks:
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
# Record timestamp of full index rebuild
|
||||
global _last_full_index_ts
|
||||
_last_full_index_ts = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# Build attachment index
|
||||
from backend.attachment_indexer import build_attachment_index
|
||||
await build_attachment_index(vault_config)
|
||||
@@ -413,7 +625,7 @@ async def build_index(progress_callback=None) -> None:
|
||||
await progress_callback("complete", {"total_vaults": len(vault_config), "total_files": total_files})
|
||||
|
||||
|
||||
async def reload_index() -> Dict[str, Any]:
|
||||
async def reload_index() -> dict[str, Any]:
|
||||
"""Force a full re-index of all vaults and return per-vault statistics.
|
||||
|
||||
Returns:
|
||||
@@ -426,7 +638,7 @@ async def reload_index() -> Dict[str, Any]:
|
||||
return stats
|
||||
|
||||
|
||||
async def reload_single_vault(vault_name: str) -> Dict[str, Any]:
|
||||
async def reload_single_vault(vault_name: str) -> dict[str, Any]:
|
||||
"""Force a re-index of a single vault and return its statistics.
|
||||
|
||||
Args:
|
||||
@@ -458,7 +670,7 @@ async def reload_single_vault(vault_name: str) -> Dict[str, Any]:
|
||||
vault_data["config"] = config
|
||||
|
||||
# Build lookup entries for the vault
|
||||
new_lookup_entries: Dict[str, List[Dict[str, str]]] = {}
|
||||
new_lookup_entries: dict[str, list[dict[str, str]]] = {}
|
||||
for f in vault_data["files"]:
|
||||
entry = {"vault": vault_name, "path": f["path"]}
|
||||
fname = f["path"].rsplit("/", 1)[-1].lower()
|
||||
@@ -489,12 +701,12 @@ async def reload_single_vault(vault_name: str) -> Dict[str, Any]:
|
||||
return stats
|
||||
|
||||
|
||||
def get_vault_names() -> List[str]:
|
||||
def get_vault_names() -> list[str]:
|
||||
"""Return the list of all indexed vault names."""
|
||||
return list(index.keys())
|
||||
|
||||
|
||||
def get_vault_data(vault_name: str) -> Optional[Dict[str, Any]]:
|
||||
def get_vault_data(vault_name: str) -> dict[str, Any] | None:
|
||||
"""Return the full index data for a vault, or ``None`` if not found."""
|
||||
return index.get(vault_name)
|
||||
|
||||
@@ -507,7 +719,7 @@ def _get_async_lock() -> asyncio.Lock:
|
||||
return _async_index_lock
|
||||
|
||||
|
||||
def _index_single_file_sync(vault_name: str, vault_path: str, file_path: str, vault_cfg: Optional[Dict[str, Any]] = None) -> Optional[Dict[str, Any]]:
|
||||
def _index_single_file_sync(vault_name: str, vault_path: str, file_path: str, vault_cfg: dict[str, Any] | None = None) -> dict[str, Any] | None:
|
||||
"""Synchronously read and parse a single file for indexing.
|
||||
|
||||
All files are indexed, including hidden files (starting with '.').
|
||||
@@ -537,19 +749,32 @@ def _index_single_file_sync(vault_name: str, vault_path: str, file_path: str, va
|
||||
|
||||
stat = fpath.stat()
|
||||
modified = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat()
|
||||
raw = fpath.read_text(encoding="utf-8", errors="replace")
|
||||
|
||||
tags: List[str] = []
|
||||
# PDF handling — binary, must go through pdf_reader (same as _scan_vault)
|
||||
tags: list[str] = []
|
||||
title = fpath.stem.replace("-", " ").replace("_", " ")
|
||||
content_preview = raw[:200].strip()
|
||||
if ext == ".pdf":
|
||||
from backend.pdf_reader import extract_pdf_metadata, extract_pdf_text
|
||||
raw = extract_pdf_text(fpath, max_chars=SEARCH_CONTENT_LIMIT)
|
||||
pdf_meta = extract_pdf_metadata(fpath)
|
||||
title = pdf_meta.get("title") or title
|
||||
content_preview = raw[:200].strip()
|
||||
elif ext == ".excalidraw" or fpath.name.lower().endswith(".excalidraw.md"):
|
||||
raw = fpath.read_text(encoding="utf-8", errors="replace")
|
||||
raw = extract_excalidraw_indexable(raw)
|
||||
title = fpath.stem.replace(".excalidraw", "").replace("-", " ").replace("_", " ")
|
||||
content_preview = raw[:200].strip()
|
||||
else:
|
||||
raw = fpath.read_text(encoding="utf-8", errors="replace")
|
||||
content_preview = raw[:200].strip()
|
||||
|
||||
if ext == ".md":
|
||||
post = parse_markdown_file(raw)
|
||||
tags = _extract_tags(post)
|
||||
inline_tags = _extract_inline_tags(post.content)
|
||||
tags = list(set(tags) | set(inline_tags))
|
||||
title = _extract_title(post, fpath)
|
||||
content_preview = post.content[:200].strip()
|
||||
if ext == ".md":
|
||||
post = parse_markdown_file(raw)
|
||||
tags = _extract_tags(post)
|
||||
inline_tags = _extract_inline_tags(post.content)
|
||||
tags = list(set(tags) | set(inline_tags))
|
||||
title = _extract_title(post, fpath)
|
||||
content_preview = post.content[:200].strip()
|
||||
|
||||
return {
|
||||
"path": str(relative).replace("\\", "/"),
|
||||
@@ -569,7 +794,7 @@ def _index_single_file_sync(vault_name: str, vault_path: str, file_path: str, va
|
||||
return None
|
||||
|
||||
|
||||
def _remove_file_from_structures(vault_name: str, rel_path: str) -> Optional[Dict[str, Any]]:
|
||||
def _remove_file_from_structures(vault_name: str, rel_path: str) -> dict[str, Any] | None:
|
||||
"""Remove a file from all index structures. Returns removed file info or None.
|
||||
|
||||
Must be called under _index_lock or _async_index_lock.
|
||||
@@ -644,7 +869,7 @@ def _ensure_parent_dirs_in_path_index(vault_name: str, rel_path: str, existing:
|
||||
})
|
||||
|
||||
|
||||
def _add_file_to_structures(vault_name: str, file_info: Dict[str, Any]):
|
||||
def _add_file_to_structures(vault_name: str, file_info: dict[str, Any]):
|
||||
"""Add a file entry to all index structures.
|
||||
|
||||
Must be called under _index_lock or _async_index_lock.
|
||||
@@ -689,7 +914,7 @@ def _add_file_to_structures(vault_name: str, file_info: Dict[str, Any]):
|
||||
_on_index_change('add', vault_name, file_info["path"], file_info) # type: ignore[misc]
|
||||
|
||||
|
||||
async def update_single_file(vault_name: str, abs_file_path: str) -> Optional[Dict[str, Any]]:
|
||||
async def update_single_file(vault_name: str, abs_file_path: str) -> dict[str, Any] | None:
|
||||
"""Re-index a single file without full rebuild.
|
||||
|
||||
Reads the file, removes the old entry if present, inserts the new one.
|
||||
@@ -736,7 +961,7 @@ async def update_single_file(vault_name: str, abs_file_path: str) -> Optional[Di
|
||||
return file_info
|
||||
|
||||
|
||||
async def remove_single_file(vault_name: str, abs_file_path: str) -> Optional[Dict[str, Any]]:
|
||||
async def remove_single_file(vault_name: str, abs_file_path: str) -> dict[str, Any] | None:
|
||||
"""Remove a single file from the index.
|
||||
|
||||
Args:
|
||||
@@ -768,7 +993,7 @@ async def remove_single_file(vault_name: str, abs_file_path: str) -> Optional[Di
|
||||
return removed
|
||||
|
||||
|
||||
async def handle_file_move(vault_name: str, src_abs: str, dest_abs: str) -> Optional[Dict[str, Any]]:
|
||||
async def handle_file_move(vault_name: str, src_abs: str, dest_abs: str) -> dict[str, Any] | None:
|
||||
"""Handle a file move/rename by removing old entry and indexing new location.
|
||||
|
||||
Args:
|
||||
@@ -817,7 +1042,7 @@ async def remove_vault_from_index(vault_name: str):
|
||||
logger.info(f"Removed vault '{vault_name}' from index")
|
||||
|
||||
|
||||
async def add_vault_to_index(vault_name: str, vault_path: str) -> Dict[str, Any]:
|
||||
async def add_vault_to_index(vault_name: str, vault_path: str) -> dict[str, Any]:
|
||||
"""Add a new vault to the index dynamically.
|
||||
|
||||
Args:
|
||||
@@ -840,7 +1065,7 @@ async def add_vault_to_index(vault_name: str, vault_path: str) -> Dict[str, Any]
|
||||
vault_data["config"] = vault_config[vault_name]
|
||||
|
||||
# Build lookup entries for the new vault
|
||||
new_lookup_entries: Dict[str, List[Dict[str, str]]] = {}
|
||||
new_lookup_entries: dict[str, list[dict[str, str]]] = {}
|
||||
for f in vault_data["files"]:
|
||||
entry = {"vault": vault_name, "path": f["path"]}
|
||||
fname = f["path"].rsplit("/", 1)[-1].lower()
|
||||
@@ -865,7 +1090,7 @@ async def add_vault_to_index(vault_name: str, vault_path: str) -> Dict[str, Any]
|
||||
return stats
|
||||
|
||||
|
||||
def find_file_in_index(link_target: str, current_vault: str) -> Optional[Dict[str, str]]:
|
||||
def find_file_in_index(link_target: str, current_vault: str) -> dict[str, str] | None:
|
||||
"""Find a file matching a wikilink target using O(1) lookup table.
|
||||
|
||||
Searches by filename first, then by full relative path.
|
||||
@@ -937,7 +1162,7 @@ def _extract_wikilinks_for_backlinks(
|
||||
})
|
||||
|
||||
|
||||
def get_backlinks(vault_name: str, file_path: str) -> List[Dict[str, str]]:
|
||||
def get_backlinks(vault_name: str, file_path: str) -> list[dict[str, str]]:
|
||||
"""Get all files that link to the given file via wikilinks.
|
||||
|
||||
Searches across all vaults for backlinks pointing to the target file.
|
||||
@@ -955,7 +1180,7 @@ def get_backlinks(vault_name: str, file_path: str) -> List[Dict[str, str]]:
|
||||
target_key += ".md"
|
||||
|
||||
results = []
|
||||
for vname, vindex in _backlink_index.items():
|
||||
for vindex in _backlink_index.values():
|
||||
bl = vindex.get(target_key, [])
|
||||
results.extend(bl)
|
||||
return results
|
||||
|
||||
+1977
-1333
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,142 @@
|
||||
"""Signed, single-use confirmation tokens for MCP mutations (Phase E4).
|
||||
|
||||
MCP has no "Apply" button, so mutating tools are exposed in two steps:
|
||||
``propose_<tool>`` returns a preview plus a **signed token**, and
|
||||
``apply_<tool>`` consumes that token to execute the mutation.
|
||||
|
||||
The token is a short-lived JWT (same secret as the app) carrying the tool name
|
||||
and its arguments. Single use is enforced by a persisted JTI blacklist, which
|
||||
also protects against replay and TOCTOU (a stale proposal cannot be re-applied).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from jose import JWTError, jwt
|
||||
|
||||
from backend.auth.jwt_handler import get_secret_key
|
||||
|
||||
logger = logging.getLogger("obsigate.mcp.confirmations")
|
||||
|
||||
ALGORITHM = "HS256"
|
||||
TOKEN_TYPE = "mcp_confirmation"
|
||||
|
||||
# Default token lifetime (seconds); override with OBSIGATE_MCP_CONFIRMATION_TTL.
|
||||
DEFAULT_TTL = int(os.environ.get("OBSIGATE_MCP_CONFIRMATION_TTL", "300"))
|
||||
|
||||
_USED_TOKENS_FILE = Path("data/mcp_used_tokens.json")
|
||||
_used_lock = threading.RLock()
|
||||
_used_loaded = False
|
||||
_used_jtis: dict[str, int] = {}
|
||||
|
||||
|
||||
class ConfirmationError(Exception):
|
||||
"""Raised when a confirmation token is invalid, expired, or already used."""
|
||||
|
||||
def __init__(self, message: str, *, code: str = "invalid_confirmation"):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.code = code
|
||||
|
||||
|
||||
def _load_used() -> None:
|
||||
"""Load used JTIs from disk once, dropping expired entries."""
|
||||
global _used_loaded, _used_jtis
|
||||
if _used_loaded:
|
||||
return
|
||||
with _used_lock:
|
||||
if _used_loaded:
|
||||
return
|
||||
if _USED_TOKENS_FILE.exists():
|
||||
try:
|
||||
data = json.loads(_USED_TOKENS_FILE.read_text(encoding="utf-8"))
|
||||
now = int(time.time())
|
||||
_used_jtis = {jti: exp for jti, exp in data.items() if int(exp) > now}
|
||||
except Exception as e: # pragma: no cover - corrupt store
|
||||
logger.warning(f"Failed to load used MCP tokens: {e}")
|
||||
_used_jtis = {}
|
||||
_used_loaded = True
|
||||
|
||||
|
||||
def _save_used() -> None:
|
||||
"""Persist used JTIs with their expiry (best-effort)."""
|
||||
try:
|
||||
_USED_TOKENS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = _USED_TOKENS_FILE.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(_used_jtis), encoding="utf-8")
|
||||
tmp.replace(_USED_TOKENS_FILE)
|
||||
except Exception as e: # pragma: no cover - disk error
|
||||
logger.warning(f"Failed to persist used MCP tokens: {e}")
|
||||
|
||||
|
||||
def create_confirmation_token(
|
||||
tool: str,
|
||||
arguments: dict[str, Any],
|
||||
username: str,
|
||||
*,
|
||||
ttl: int | None = None,
|
||||
) -> str:
|
||||
"""Create a signed confirmation token for a pending mutation."""
|
||||
now = int(time.time())
|
||||
payload = {
|
||||
"type": TOKEN_TYPE,
|
||||
"tool": tool,
|
||||
"arguments": arguments,
|
||||
"sub": username,
|
||||
"jti": str(uuid.uuid4()),
|
||||
"iat": now,
|
||||
"exp": now + (ttl if ttl is not None else DEFAULT_TTL),
|
||||
}
|
||||
return jwt.encode(payload, get_secret_key(), algorithm=ALGORITHM)
|
||||
|
||||
|
||||
def peek_confirmation_token(token: str) -> dict[str, Any]:
|
||||
"""Decode and validate a token without consuming it (signature + expiry)."""
|
||||
try:
|
||||
payload = jwt.decode(token, get_secret_key(), algorithms=[ALGORITHM])
|
||||
except JWTError as e:
|
||||
raise ConfirmationError("Invalid or expired confirmation token", code="invalid_confirmation") from e
|
||||
|
||||
if payload.get("type") != TOKEN_TYPE:
|
||||
raise ConfirmationError("Wrong token type", code="invalid_confirmation")
|
||||
if not payload.get("tool") or not payload.get("sub"):
|
||||
raise ConfirmationError("Malformed confirmation token", code="invalid_confirmation")
|
||||
return payload
|
||||
|
||||
|
||||
def consume_confirmation_token(token: str, username: str) -> dict[str, Any]:
|
||||
"""Validate a token, enforce single use, and return its payload.
|
||||
|
||||
Raises:
|
||||
ConfirmationError: invalid/expired token, wrong user, or replay.
|
||||
"""
|
||||
payload = peek_confirmation_token(token)
|
||||
|
||||
if payload.get("sub") != username:
|
||||
raise ConfirmationError("Confirmation token does not belong to this user", code="forbidden")
|
||||
|
||||
jti = payload.get("jti")
|
||||
if not jti:
|
||||
raise ConfirmationError("Malformed confirmation token", code="invalid_confirmation")
|
||||
|
||||
_load_used()
|
||||
now = int(time.time())
|
||||
with _used_lock:
|
||||
if jti in _used_jtis:
|
||||
raise ConfirmationError("Confirmation token already used", code="token_reused")
|
||||
# Record as used *before* returning so a concurrent replay is rejected.
|
||||
_used_jtis[jti] = int(payload.get("exp", now))
|
||||
# Opportunistic cleanup of expired JTIs.
|
||||
for old_jti in [k for k, exp in _used_jtis.items() if exp <= now]:
|
||||
_used_jtis.pop(old_jti, None)
|
||||
_save_used()
|
||||
|
||||
return payload
|
||||
@@ -0,0 +1,485 @@
|
||||
"""ObsiGate MCP server — Streamable HTTP transport (Phase E).
|
||||
|
||||
Exposes the shared AI tool layer (``backend.tools``) to external MCP clients
|
||||
(Claude Desktop, Cursor…). The same registry that powers the in-app assistant
|
||||
is registered here, so the two fronts never diverge.
|
||||
|
||||
Primitives:
|
||||
- **Tools** — read/search tools directly; mutating tools as a two-step
|
||||
``propose_<tool>`` / ``apply_<tool>`` pair (signed, single-use token).
|
||||
- **Resources** — accessible vaults (``vault://<name>``) and files
|
||||
(``vault://<name>/<path>``), read-only and secret-redacted.
|
||||
- **Prompts** — reusable note/summary templates.
|
||||
|
||||
Transport: Streamable HTTP mounted at ``/mcp`` (auth ``Authorization: Bearer
|
||||
<JWT>``). ``stdio`` is left for later.
|
||||
|
||||
The transport manager is started lazily on the first request so the endpoint
|
||||
also works in tests (where the ASGI lifespan is not run).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import difflib
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
from mcp import types
|
||||
from mcp.server.lowlevel import Server
|
||||
from mcp.server.lowlevel.helper_types import ReadResourceContents
|
||||
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
|
||||
from pydantic import AnyUrl
|
||||
from starlette._utils import get_route_path
|
||||
from starlette.requests import Request
|
||||
from starlette.routing import BaseRoute, Match
|
||||
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||
|
||||
from backend.auth.middleware import get_current_user, is_auth_enabled
|
||||
from backend.services.files import read_file_text
|
||||
from backend.services.vaults import list_accessible_vaults
|
||||
from backend.tools.api import (
|
||||
ToolContext,
|
||||
ToolError,
|
||||
ToolMode,
|
||||
ToolRisk,
|
||||
ToolScope,
|
||||
call_tool,
|
||||
get_tool,
|
||||
list_tools,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("obsigate.mcp")
|
||||
|
||||
SERVER_NAME = "obsigate"
|
||||
SERVER_INSTRUCTIONS = (
|
||||
"ObsiGate exposes your Obsidian vaults: read, search and (with confirmation) "
|
||||
"create, edit, rename, move or delete notes. Mutating tools require the "
|
||||
"two-step propose_/apply_ flow."
|
||||
)
|
||||
|
||||
# Cap on the file size returned by resources (bytes).
|
||||
MAX_RESOURCE_BYTES = 200_000
|
||||
|
||||
|
||||
def _anonymous_user() -> dict[str, Any]:
|
||||
return {
|
||||
"username": "anonymous",
|
||||
"display_name": "Anonymous",
|
||||
"role": "admin",
|
||||
"vaults": ["*"],
|
||||
"active": True,
|
||||
"_token_vaults": ["*"],
|
||||
}
|
||||
|
||||
|
||||
def _authenticate(request: Request) -> dict[str, Any] | None:
|
||||
"""Resolve the caller from the ``Authorization: Bearer`` header."""
|
||||
if not is_auth_enabled():
|
||||
return _anonymous_user()
|
||||
|
||||
from fastapi.security import HTTPAuthorizationCredentials
|
||||
|
||||
header = request.headers.get("authorization", "")
|
||||
if not header.lower().startswith("bearer "):
|
||||
return None
|
||||
credentials = HTTPAuthorizationCredentials(scheme="Bearer", credentials=header[7:].strip())
|
||||
return get_current_user(request, credentials)
|
||||
|
||||
|
||||
def _user_from_context(server: Server) -> dict[str, Any]:
|
||||
"""Return the authenticated user attached to the current request scope."""
|
||||
request = server.request_context.request
|
||||
if request is None: # pragma: no cover - stdio not supported yet
|
||||
raise ToolError("No request context available", code="no_request_context")
|
||||
user = getattr(request.state, "user", None)
|
||||
if not user:
|
||||
raise ToolError("Unauthenticated MCP request", code="unauthenticated")
|
||||
return user
|
||||
|
||||
|
||||
def _text(payload: Any) -> list[types.Content]:
|
||||
"""Serialize a payload as a single text content block."""
|
||||
return [types.TextContent(type="text", text=json.dumps(payload, ensure_ascii=False, default=str))]
|
||||
|
||||
|
||||
def _unified_diff(old: str, new: str) -> str:
|
||||
diff = difflib.unified_diff(
|
||||
old.splitlines(keepends=True),
|
||||
new.splitlines(keepends=True),
|
||||
fromfile="current",
|
||||
tofile="proposed",
|
||||
)
|
||||
return "".join(diff)
|
||||
|
||||
|
||||
def _build_preview(spec: Any, params: Any) -> dict[str, Any]:
|
||||
"""Build a human-readable preview for a proposed mutation."""
|
||||
arguments = params.model_dump()
|
||||
preview: dict[str, Any] = {"tool": spec.name, "arguments": arguments}
|
||||
|
||||
if spec.name in ("create_file", "edit_file", "append_to_file"):
|
||||
vault = arguments.get("vault")
|
||||
path = arguments.get("path", "")
|
||||
content = arguments.get("content", "")
|
||||
try:
|
||||
current = read_file_text(vault, path, redact=True, max_bytes=MAX_RESOURCE_BYTES)["content"]
|
||||
except Exception:
|
||||
current = ""
|
||||
if spec.name == "append_to_file":
|
||||
separator = "" if (not current or current.endswith("\n")) else "\n"
|
||||
proposed = current + separator + content
|
||||
else:
|
||||
proposed = content
|
||||
preview["diff"] = _unified_diff(current, proposed)
|
||||
|
||||
return preview
|
||||
|
||||
|
||||
def _tool_definitions() -> list[types.Tool]:
|
||||
"""Build the MCP tool list from the shared registry."""
|
||||
definitions: list[types.Tool] = []
|
||||
for spec in list_tools(scope=ToolScope.MCP):
|
||||
if spec.risk == ToolRisk.READ:
|
||||
definitions.append(
|
||||
types.Tool(
|
||||
name=spec.name,
|
||||
description=spec.description,
|
||||
inputSchema=spec.parameters_schema(),
|
||||
annotations=types.ToolAnnotations(readOnlyHint=True, openWorldHint=False),
|
||||
)
|
||||
)
|
||||
continue
|
||||
|
||||
destructive = spec.risk == ToolRisk.DANGEROUS
|
||||
definitions.append(
|
||||
types.Tool(
|
||||
name=f"propose_{spec.name}",
|
||||
description=(
|
||||
f"Propose to run '{spec.name}' and return a confirmation token. "
|
||||
"No change is made until apply_ is called."
|
||||
),
|
||||
inputSchema=spec.parameters_schema(),
|
||||
annotations=types.ToolAnnotations(
|
||||
readOnlyHint=True, destructiveHint=False, idempotentHint=True, openWorldHint=False
|
||||
),
|
||||
)
|
||||
)
|
||||
definitions.append(
|
||||
types.Tool(
|
||||
name=f"apply_{spec.name}",
|
||||
description=f"Apply a previously proposed '{spec.name}' using its confirmation token.",
|
||||
inputSchema={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"confirmation_token": {
|
||||
"type": "string",
|
||||
"description": f"Token returned by propose_{spec.name}",
|
||||
}
|
||||
},
|
||||
"required": ["confirmation_token"],
|
||||
},
|
||||
annotations=types.ToolAnnotations(
|
||||
readOnlyHint=False, destructiveHint=destructive, idempotentHint=False, openWorldHint=False
|
||||
),
|
||||
)
|
||||
)
|
||||
return definitions
|
||||
|
||||
|
||||
def _parse_vault_uri(uri: str) -> tuple[str, str]:
|
||||
"""Split ``vault://name/path`` into ``(vault, path)``."""
|
||||
parsed = urlparse(uri)
|
||||
if parsed.scheme != "vault" or not parsed.netloc:
|
||||
raise ValueError(f"Unsupported resource URI: {uri}")
|
||||
return parsed.netloc, parsed.path.lstrip("/")
|
||||
|
||||
|
||||
def build_server() -> Server:
|
||||
"""Create and configure the low-level MCP server."""
|
||||
from backend.version import get_version
|
||||
|
||||
server: Server = Server(SERVER_NAME, version=get_version(), instructions=SERVER_INSTRUCTIONS)
|
||||
|
||||
# ── Tools ──────────────────────────────────────────────────────────
|
||||
@server.list_tools()
|
||||
async def _list_tools() -> list[types.Tool]:
|
||||
return _tool_definitions()
|
||||
|
||||
@server.call_tool()
|
||||
async def _call_tool(name: str, arguments: dict[str, Any]) -> list[types.Content]:
|
||||
user = _user_from_context(server)
|
||||
|
||||
if name.startswith("propose_"):
|
||||
return _propose(user, name[len("propose_"):], arguments)
|
||||
if name.startswith("apply_"):
|
||||
return _apply(user, name[len("apply_"):], arguments)
|
||||
|
||||
spec = get_tool(name)
|
||||
if spec is None:
|
||||
raise ValueError(f"Unknown tool: {name}")
|
||||
if spec.risk != ToolRisk.READ:
|
||||
raise ValueError(f"Tool '{name}' is mutating; use propose_{name}/apply_{name}")
|
||||
|
||||
ctx = ToolContext(user=user, mode=ToolMode.MCP)
|
||||
try:
|
||||
result = call_tool(name, ctx, arguments)
|
||||
except ToolError as e:
|
||||
return _text(e.to_dict())
|
||||
return _text({"ok": True, "data": result.data})
|
||||
|
||||
# ── Resources ──────────────────────────────────────────────────────
|
||||
@server.list_resources()
|
||||
async def _list_resources() -> list[types.Resource]:
|
||||
user = _user_from_context(server)
|
||||
return [
|
||||
types.Resource(
|
||||
uri=AnyUrl(f"vault://{v['name']}"),
|
||||
name=v["name"],
|
||||
description=f"ObsiGate vault '{v['name']}' ({v['file_count']} files)",
|
||||
mimeType="application/x-obsigate-vault",
|
||||
)
|
||||
for v in list_accessible_vaults(user)
|
||||
]
|
||||
|
||||
@server.list_resource_templates()
|
||||
async def _list_resource_templates() -> list[types.ResourceTemplate]:
|
||||
return [
|
||||
types.ResourceTemplate(
|
||||
uriTemplate="vault://{vault}/{path}",
|
||||
name="Vault file",
|
||||
description="Read a text file from a vault (secrets redacted)",
|
||||
mimeType="text/markdown",
|
||||
)
|
||||
]
|
||||
|
||||
@server.read_resource()
|
||||
async def _read_resource(uri: Any) -> list[ReadResourceContents]:
|
||||
user = _user_from_context(server)
|
||||
vault, path = _parse_vault_uri(str(uri))
|
||||
ctx = ToolContext(user=user, mode=ToolMode.MCP)
|
||||
ctx.require_vault_access(vault)
|
||||
data = read_file_text(vault, path, redact=True, max_bytes=MAX_RESOURCE_BYTES)
|
||||
mime = "text/markdown" if Path(path).suffix.lower() == ".md" else "text/plain"
|
||||
return [ReadResourceContents(content=data["content"], mime_type=mime)]
|
||||
|
||||
# ── Prompts ────────────────────────────────────────────────────────
|
||||
@server.list_prompts()
|
||||
async def _list_prompts() -> list[types.Prompt]:
|
||||
return [
|
||||
types.Prompt(
|
||||
name="summarize-directory",
|
||||
description="Summarize every note in a vault directory.",
|
||||
arguments=[
|
||||
types.PromptArgument(name="vault", description="Vault name", required=True),
|
||||
types.PromptArgument(name="path", description="Directory path (empty = root)", required=False),
|
||||
],
|
||||
),
|
||||
types.Prompt(
|
||||
name="generate-note",
|
||||
description="Draft a new note on a topic, using the vault for context.",
|
||||
arguments=[
|
||||
types.PromptArgument(name="topic", description="Note topic", required=True),
|
||||
types.PromptArgument(name="vault", description="Target vault name", required=True),
|
||||
],
|
||||
),
|
||||
types.Prompt(
|
||||
name="find-related",
|
||||
description="Find notes related to a given note.",
|
||||
arguments=[
|
||||
types.PromptArgument(name="vault", description="Vault name", required=True),
|
||||
types.PromptArgument(name="path", description="Reference note path", required=True),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
@server.get_prompt()
|
||||
async def _get_prompt(name: str, arguments: dict[str, str] | None) -> types.GetPromptResult:
|
||||
args = arguments or {}
|
||||
|
||||
def _require(key: str) -> str:
|
||||
value = (args.get(key) or "").strip()
|
||||
if not value:
|
||||
raise ValueError(f"Missing required prompt argument: {key}")
|
||||
return value
|
||||
|
||||
if name == "summarize-directory":
|
||||
vault = _require("vault")
|
||||
path = (args.get("path") or "").strip()
|
||||
text = (
|
||||
f"List the directory '{path or '/'}' of vault '{vault}' (use list_directory), "
|
||||
"read the notes it contains, then write a concise summary with the key ideas."
|
||||
)
|
||||
elif name == "generate-note":
|
||||
topic = _require("topic")
|
||||
vault = _require("vault")
|
||||
text = (
|
||||
f"Draft a well-structured Markdown note about '{topic}' in vault '{vault}'. "
|
||||
"Search the vault first for existing material, then propose the note content."
|
||||
)
|
||||
elif name == "find-related":
|
||||
vault = _require("vault")
|
||||
path = _require("path")
|
||||
text = (
|
||||
f"Read '{path}' in vault '{vault}', then find related notes via backlinks and "
|
||||
"full-text search. Return a short list with why each note is related."
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"Unknown prompt: {name}")
|
||||
|
||||
return types.GetPromptResult(
|
||||
description=f"ObsiGate prompt: {name}",
|
||||
messages=[types.PromptMessage(role="user", content=types.TextContent(type="text", text=text))],
|
||||
)
|
||||
|
||||
return server
|
||||
|
||||
|
||||
def _propose(user: dict[str, Any], tool: str, arguments: dict[str, Any]) -> list[types.Content]:
|
||||
"""Validate a mutation, return a preview and a confirmation token."""
|
||||
from backend.mcp.confirmations import DEFAULT_TTL, create_confirmation_token
|
||||
|
||||
spec = get_tool(tool)
|
||||
if spec is None:
|
||||
raise ValueError(f"Unknown tool: {tool}")
|
||||
if spec.risk == ToolRisk.READ:
|
||||
raise ValueError(f"Tool '{tool}' is read-only; call it directly")
|
||||
|
||||
params = spec.input_model.model_validate(arguments)
|
||||
vault = getattr(params, "vault", None)
|
||||
ctx = ToolContext(user=user, mode=ToolMode.MCP)
|
||||
if vault and vault != "all":
|
||||
ctx.require_vault_access(vault)
|
||||
if spec.risk == ToolRisk.DANGEROUS:
|
||||
ctx.require_destructive_allowed(vault)
|
||||
|
||||
token = create_confirmation_token(tool, arguments, ctx.username)
|
||||
payload = _build_preview(spec, params)
|
||||
payload["confirmation_token"] = token
|
||||
payload["expires_in"] = DEFAULT_TTL
|
||||
return _text(payload)
|
||||
|
||||
|
||||
def _apply(user: dict[str, Any], tool: str, arguments: dict[str, Any]) -> list[types.Content]:
|
||||
"""Consume a confirmation token and execute the mutation."""
|
||||
from backend.mcp.confirmations import ConfirmationError, consume_confirmation_token
|
||||
|
||||
token = (arguments or {}).get("confirmation_token")
|
||||
if not token:
|
||||
raise ValueError("Missing 'confirmation_token'")
|
||||
|
||||
try:
|
||||
payload = consume_confirmation_token(token, user.get("username", ""))
|
||||
except ConfirmationError as e:
|
||||
return _text({"ok": False, "error": {"code": e.code, "message": e.message}})
|
||||
|
||||
if payload.get("tool") != tool:
|
||||
return _text(
|
||||
{
|
||||
"ok": False,
|
||||
"error": {
|
||||
"code": "token_mismatch",
|
||||
"message": f"Token was issued for '{payload.get('tool')}', not '{tool}'",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
ctx = ToolContext(user=user, mode=ToolMode.MCP, confirmed=True)
|
||||
try:
|
||||
result = call_tool(tool, ctx, payload.get("arguments") or {}, confirm=True)
|
||||
except ToolError as e:
|
||||
return _text(e.to_dict())
|
||||
return _text({"ok": True, "data": result.data})
|
||||
|
||||
|
||||
class McpASGIApp:
|
||||
"""ASGI wrapper: authenticates the request, then runs the MCP transport.
|
||||
|
||||
The session manager is started lazily on first use and kept alive for the
|
||||
process lifetime, so the endpoint works both under uvicorn (with lifespan)
|
||||
and under the test client (without entering the ASGI lifespan).
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._manager: StreamableHTTPSessionManager | None = None
|
||||
self._start_lock: asyncio.Lock | None = None
|
||||
self._run_task: asyncio.Task | None = None
|
||||
|
||||
def _get_manager(self) -> StreamableHTTPSessionManager:
|
||||
if self._manager is None:
|
||||
self._manager = StreamableHTTPSessionManager(
|
||||
app=build_server(),
|
||||
json_response=True,
|
||||
stateless=False,
|
||||
)
|
||||
return self._manager
|
||||
|
||||
async def _ensure_started(self) -> StreamableHTTPSessionManager:
|
||||
manager = self._get_manager()
|
||||
if getattr(manager, "_task_group", None) is not None:
|
||||
return manager
|
||||
if self._start_lock is None:
|
||||
self._start_lock = asyncio.Lock()
|
||||
async with self._start_lock:
|
||||
if getattr(manager, "_task_group", None) is None:
|
||||
self._run_task = asyncio.create_task(self._run_manager(manager))
|
||||
for _ in range(500):
|
||||
if getattr(manager, "_task_group", None) is not None:
|
||||
break
|
||||
await asyncio.sleep(0.005)
|
||||
return manager
|
||||
|
||||
async def _run_manager(self, manager: StreamableHTTPSessionManager) -> None:
|
||||
async with manager.run():
|
||||
await asyncio.Event().wait()
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
if scope["type"] != "http":
|
||||
return
|
||||
|
||||
request = Request(scope, receive)
|
||||
user = _authenticate(request)
|
||||
if user is None:
|
||||
response = JSONResponse(
|
||||
{"detail": "Authentification requise"},
|
||||
status_code=401,
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
await response(scope, receive, send)
|
||||
return
|
||||
|
||||
scope.setdefault("state", {})["user"] = user
|
||||
manager = await self._ensure_started()
|
||||
await manager.handle_request(scope, receive, send)
|
||||
|
||||
|
||||
# Module-level singleton mounted by ``backend.main`` at ``/mcp``.
|
||||
mcp_app = McpASGIApp()
|
||||
|
||||
|
||||
class McpMount(BaseRoute):
|
||||
"""ASGI route matching ``/mcp`` and ``/mcp/...`` (unlike Starlette's Mount).
|
||||
|
||||
Starlette's :class:`~starlette.routing.Mount` compiles ``/mcp/{path:path}``
|
||||
and therefore does **not** match the bare ``/mcp`` path used by MCP clients.
|
||||
This route matches both forms and delegates to :data:`mcp_app` unchanged.
|
||||
"""
|
||||
|
||||
def __init__(self, app: ASGIApp, path: str = "/mcp") -> None:
|
||||
self.app = app
|
||||
self.path = path.rstrip("/")
|
||||
|
||||
def matches(self, scope: Scope) -> tuple[Match, Scope]:
|
||||
if scope["type"] == "http":
|
||||
route_path = get_route_path(scope)
|
||||
if route_path == self.path or route_path.startswith(self.path + "/"):
|
||||
return Match.FULL, {"endpoint": self.app}
|
||||
return Match.NONE, {}
|
||||
|
||||
async def handle(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
await self.app(scope, receive, send)
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
"""Model-capability metadata for the AI assistant.
|
||||
|
||||
Two layers, in order of trust:
|
||||
|
||||
1. **Provider-declared** (:mod:`backend.provider_capabilities`) — when the
|
||||
provider publishes per-model capabilities in its models endpoint (Mistral
|
||||
``capabilities``, OpenRouter ``architecture``), that declaration *wins* for
|
||||
every flag it mentions. The snapshot is cached by the model-list endpoint.
|
||||
2. **Curated table** (this module) — a static, hand-maintained map of known
|
||||
model-name patterns to capability flags with per-provider defaults. It fills
|
||||
the flags the provider stays silent about, and is the only source for
|
||||
providers and models that declare nothing (offline, no API key, DeepSeek,
|
||||
NVIDIA, QwenCloud, Xiaomi…).
|
||||
|
||||
The UI uses the result to show, when a model is selected, which features it
|
||||
supports (Chat, Embeddings, Rerank, Images, Video, Audio Speech, Audio
|
||||
Transcriptions, Vision).
|
||||
|
||||
The curated layer is intentionally conservative: an unknown model falls back to
|
||||
the provider default (usually ``chat`` only), so we never claim a capability the
|
||||
model may not have. A provider declaration, on the other hand, is authoritative
|
||||
in both directions — it can also *revoke* a flag the curated table guessed
|
||||
wrongly (e.g. ``mistral-embed`` declares no chat).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.provider_capabilities import get_declared_capabilities
|
||||
|
||||
# Ordered list of capability keys exposed to the UI. Keep in sync with the
|
||||
# frontend ``AI_CAPABILITY_KEYS`` and the i18n ``ai.cap_*`` labels.
|
||||
CAPABILITY_KEYS: tuple[str, ...] = (
|
||||
"chat",
|
||||
"embeddings",
|
||||
"rerank",
|
||||
"images",
|
||||
"video",
|
||||
"audio_speech",
|
||||
"audio_transcription",
|
||||
"vision",
|
||||
)
|
||||
|
||||
|
||||
def _caps(**kwargs: bool) -> dict[str, bool]:
|
||||
"""Build a full capability dict (missing flags default to False)."""
|
||||
base = {key: False for key in CAPABILITY_KEYS}
|
||||
base.update(kwargs)
|
||||
return base
|
||||
|
||||
|
||||
# Provider defaults, used when no model-name pattern matches.
|
||||
_PROVIDER_DEFAULTS: dict[str, dict[str, bool]] = {
|
||||
"deepseek": _caps(chat=True),
|
||||
"openrouter": _caps(chat=True),
|
||||
"gemini": _caps(chat=True, vision=True, embeddings=True, audio_speech=True),
|
||||
"ollama": _caps(chat=True, embeddings=True),
|
||||
"nvidia": _caps(chat=True),
|
||||
"qwencloud": _caps(chat=True),
|
||||
"xiaomi": _caps(chat=True),
|
||||
# Mistral: chat only. ``embeddings`` used to be assumed for every Mistral
|
||||
# model, which wrongly labelled mistral-large / codestral as embedders
|
||||
# (BUG-044); the ``embed`` rule below covers the real embedding models.
|
||||
"mistral": _caps(chat=True),
|
||||
}
|
||||
|
||||
# Ordered (substrings, capabilities) rules — the first matching rule wins.
|
||||
# More specific modalities are listed before the broad vision/chat rule.
|
||||
_MODEL_RULES: list[tuple[tuple[str, ...], dict[str, bool]]] = [
|
||||
# Rerankers.
|
||||
(("rerank", "cross-encoder"), _caps(rerank=True)),
|
||||
# Embedding models (usually not chat-capable).
|
||||
(
|
||||
("text-embedding", "embed", "bge-", "e5-", "nomic-embed", "gte-"),
|
||||
_caps(embeddings=True),
|
||||
),
|
||||
# Speech-to-text / transcription.
|
||||
(
|
||||
("whisper", "transcrib", "-asr", "asr-", "speech-to-text"),
|
||||
_caps(audio_transcription=True),
|
||||
),
|
||||
# Text-to-speech.
|
||||
(
|
||||
("-tts", "text-to-speech", "voiceclone", "voicedesign"),
|
||||
_caps(audio_speech=True),
|
||||
),
|
||||
# Image generation.
|
||||
(
|
||||
("dall-e", "stable-diffusion", "flux", "imagen", "image-gen"),
|
||||
_caps(images=True),
|
||||
),
|
||||
# Video generation.
|
||||
(("veo-", "sora", "video-gen", "-video"), _caps(video=True)),
|
||||
# Document OCR models — image input, but not a chat endpoint.
|
||||
(("mistral-ocr",), _caps(vision=True)),
|
||||
# Vision-capable chat models (multimodal input).
|
||||
(
|
||||
(
|
||||
"gpt-4o",
|
||||
"gpt-4.1",
|
||||
"gpt-5",
|
||||
"o3",
|
||||
"o4",
|
||||
"claude-3",
|
||||
"claude-4",
|
||||
"gemini",
|
||||
"qwen-vl",
|
||||
"-vl-",
|
||||
"vl-",
|
||||
"vision",
|
||||
"llava",
|
||||
"pixtral",
|
||||
"internvl",
|
||||
"minicpm-v",
|
||||
"llama-3.2-vision",
|
||||
"mimo-vl",
|
||||
# Mistral vision families (BUG-044): text-only mistral-large,
|
||||
# codestral and voxtral are deliberately absent.
|
||||
"ministral",
|
||||
"magistral",
|
||||
"mistral-small",
|
||||
"mistral-medium",
|
||||
"mistral-vibe-cli",
|
||||
"labs-leanstral",
|
||||
),
|
||||
_caps(chat=True, vision=True),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def _curated_capabilities(provider: str, model: str) -> dict[str, bool]:
|
||||
"""Curated table lookup (model rules first, then the provider default)."""
|
||||
if model:
|
||||
for needles, caps in _MODEL_RULES:
|
||||
if any(needle in model for needle in needles):
|
||||
return dict(caps)
|
||||
return dict(_PROVIDER_DEFAULTS.get(provider, _caps(chat=True)))
|
||||
|
||||
|
||||
def get_model_capabilities(provider: str, model: str) -> dict[str, bool]:
|
||||
"""Return the capability flags for a ``provider``/``model`` pair.
|
||||
|
||||
A provider declaration (see :mod:`backend.provider_capabilities`) overrides
|
||||
the curated table for every flag it mentions; the curated table supplies the
|
||||
rest.
|
||||
|
||||
Args:
|
||||
provider: Provider identifier (e.g. ``"deepseek"``). Case-insensitive.
|
||||
model: Model identifier (e.g. ``"deepseek-chat"``). May be empty, in
|
||||
which case the provider default is returned.
|
||||
|
||||
Returns:
|
||||
A dict with every key of :data:`CAPABILITY_KEYS` and boolean values.
|
||||
"""
|
||||
provider = (provider or "").strip().lower()
|
||||
name = (model or "").strip().lower()
|
||||
caps = _curated_capabilities(provider, name)
|
||||
declared = get_declared_capabilities(provider, name)
|
||||
if declared:
|
||||
caps.update({key: value for key, value in declared.items() if key in CAPABILITY_KEYS})
|
||||
return caps
|
||||
|
||||
|
||||
def get_capabilities_for_models(
|
||||
provider: str, models: list[str]
|
||||
) -> dict[str, dict[str, bool]]:
|
||||
"""Return a ``{model: capabilities}`` map for a list of models."""
|
||||
return {model: get_model_capabilities(provider, model) for model in models}
|
||||
|
||||
|
||||
def model_supports_vision(provider: str, model: str) -> bool:
|
||||
"""True when the given model can accept image input."""
|
||||
return bool(get_model_capabilities(provider, model).get("vision"))
|
||||
|
||||
|
||||
def capabilities_payload(provider: str, model: str) -> dict[str, Any]:
|
||||
"""Serialize capabilities for API responses."""
|
||||
return {
|
||||
"provider": provider,
|
||||
"model": model,
|
||||
"capabilities": get_model_capabilities(provider, model),
|
||||
}
|
||||
@@ -0,0 +1,526 @@
|
||||
"""OpenAPI 3.1 documentation helpers for ObsiGate (#72).
|
||||
|
||||
This module keeps the API-documentation concerns out of ``backend/main.py``:
|
||||
|
||||
* :data:`TAGS_METADATA` — human descriptions and ordering for every tag.
|
||||
* :func:`enrich_openapi_schema` — post-processes the schema generated by
|
||||
FastAPI to assign tags, add servers/security/examples and document common
|
||||
error responses.
|
||||
* :func:`render_api_landing` — a self-contained ``/api`` landing page that
|
||||
reads ``/openapi.json`` at runtime and groups endpoints by tag, with direct
|
||||
links to the Swagger UI (``/docs``) and ReDoc (``/redoc``).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tag metadata
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
TAGS_METADATA: list[dict[str, str]] = [
|
||||
{"name": "System", "description": "Health checks, runtime configuration, diagnostics, dashboard stats and the SSE event stream."},
|
||||
{"name": "Auth", "description": "Authentication, sessions, user profile, MFA (TOTP/WebAuthn) and API keys."},
|
||||
{"name": "Files", "description": "Browse, read, create, rename, move, delete and download vault files."},
|
||||
{"name": "PDF", "description": "PDF metadata and byte-range streaming for inline viewing."},
|
||||
{"name": "Vaults", "description": "List, add and remove vaults; per-vault display settings; attachment indexing."},
|
||||
{"name": "Search", "description": "Full-text and advanced search, tags, title suggestions and the link graph."},
|
||||
{"name": "Bookmarks", "description": "Recently opened files, bookmarks and saved searches."},
|
||||
{"name": "Backups", "description": "Automatic file backups, diffs, restore, compression and purge."},
|
||||
{"name": "Export", "description": "Export notes or whole vaults to HTML, Markdown bundle or ePub."},
|
||||
{"name": "AI", "description": "AI-powered editor actions, provider status and model discovery."},
|
||||
{"name": "BooksLM", "description": "Directory-scoped AI chat (NotebookLM-style) over a vault folder."},
|
||||
{"name": "MCP", "description": "Model Context Protocol server (Streamable HTTP) exposing the shared AI tool layer to external clients (Claude Desktop, Cursor…)."},
|
||||
{"name": "Collaboration", "description": "Real-time collaborative editing over WebSocket (`/ws/collab/{vault}/{path}`): Yjs/CRDT updates, awareness (cursors) and debounced server-side persistence."},
|
||||
{"name": "Sharing", "description": "Create and manage public read-only share links for documents."},
|
||||
{"name": "Webhooks", "description": "HTTP callbacks signed with HMAC-SHA256 for file events."},
|
||||
{"name": "Conflicts", "description": "Detect and resolve Syncthing sync-conflict files."},
|
||||
{"name": "Admin", "description": "Admin-only system monitoring: stats, audit log, backup stats and live stream."},
|
||||
{"name": "Plugins", "description": "Install, enable and manage user plugins."},
|
||||
{"name": "Push", "description": "Web Push (VAPID) subscription management and test notifications."},
|
||||
{"name": "Frontend", "description": "Static assets and SPA fallback routes."},
|
||||
]
|
||||
|
||||
API_DESCRIPTION = """
|
||||
**ObsiGate** exposes a REST API covering the entire application: vaults, files,
|
||||
full-text search, backups, exports, AI actions, sharing and administration.
|
||||
|
||||
### Authentication
|
||||
When authentication is enabled, send a bearer token obtained from
|
||||
`POST /api/auth/login`:
|
||||
|
||||
```
|
||||
Authorization: Bearer <access_token>
|
||||
```
|
||||
|
||||
The same token is also accepted as an HTTP-only cookie, so browser clients can
|
||||
simply use `credentials: "include"`.
|
||||
|
||||
### Real-time collaboration
|
||||
Besides the REST API, a WebSocket endpoint `GET /ws/collab/{vault}/{path}` (upgrade) powers
|
||||
simultaneous editing of the same file: clients exchange Yjs/CRDT updates and awareness (remote
|
||||
cursors), and the server persists the document 2 s after the last change. Authentication uses the
|
||||
`access_token` cookie (or a `token` query parameter) and vault access is enforced per connection.
|
||||
See the collaboration feature documentation (`docs/features/collaboration.md`) for the protocol.
|
||||
|
||||
### Interactive documentation
|
||||
* **Swagger UI** — [/docs](/docs): try requests directly from the browser.
|
||||
* **ReDoc** — [/redoc](/redoc): clean, reading-oriented reference.
|
||||
* **OpenAPI JSON** — [/openapi.json](/openapi.json): machine-readable schema.
|
||||
|
||||
### Errors
|
||||
Errors use the standard FastAPI envelope `{"detail": "..."}` with the
|
||||
appropriate HTTP status (`400`, `401`, `403`, `404`, `409`, `422`, `500`).
|
||||
"""
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Automatic tag assignment
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
# Rules are evaluated in order; the first match wins. Keep the most specific
|
||||
# prefixes first (e.g. BooksLM before AI, config/ai-* before config).
|
||||
_TAG_RULES: list[tuple[re.Pattern[str], str]] = [
|
||||
(re.compile(r"^/api/auth"), "Auth"),
|
||||
(re.compile(r"^/api/admin"), "Admin"),
|
||||
(re.compile(r"^/api/plugins"), "Plugins"),
|
||||
(re.compile(r"^/api/push"), "Push"),
|
||||
(re.compile(r"^/api/ai/bookslm"), "BooksLM"),
|
||||
(re.compile(r"^/api/ai"), "AI"),
|
||||
(re.compile(r"^/mcp"), "MCP"),
|
||||
(re.compile(r"^/api/config/ai-"), "AI"),
|
||||
(re.compile(r"^/api/share"), "Sharing"),
|
||||
(re.compile(r"^/api/shares"), "Sharing"),
|
||||
(re.compile(r"^/s/"), "Sharing"),
|
||||
(re.compile(r"^/api/webhooks"), "Webhooks"),
|
||||
(re.compile(r"^/api/conflicts"), "Conflicts"),
|
||||
(re.compile(r"^/api/backups"), "Backups"),
|
||||
(re.compile(r"^/api/file/[^/]+/(backups|diff|restore)"), "Backups"),
|
||||
(re.compile(r"^/api/export"), "Export"),
|
||||
(re.compile(r"^/api/file/[^/]+/pdf"), "PDF"),
|
||||
(re.compile(r"^/api/search"), "Search"),
|
||||
(re.compile(r"^/api/tags"), "Search"),
|
||||
(re.compile(r"^/api/tree-search"), "Search"),
|
||||
(re.compile(r"^/api/suggest"), "Search"),
|
||||
(re.compile(r"^/api/graph"), "Search"),
|
||||
(re.compile(r"^/api/recent"), "Bookmarks"),
|
||||
(re.compile(r"^/api/bookmarks"), "Bookmarks"),
|
||||
(re.compile(r"^/api/saved-searches"), "Bookmarks"),
|
||||
(re.compile(r"^/api/vaults"), "Vaults"),
|
||||
(re.compile(r"^/api/vault/"), "Vaults"),
|
||||
(re.compile(r"^/api/index/reload"), "Vaults"),
|
||||
(re.compile(r"^/api/attachments"), "Vaults"),
|
||||
(re.compile(r"^/api/browse"), "Files"),
|
||||
(re.compile(r"^/api/file"), "Files"),
|
||||
(re.compile(r"^/api/directory"), "Files"),
|
||||
(re.compile(r"^/api/move"), "Files"),
|
||||
(re.compile(r"^/api/image"), "Files"),
|
||||
(re.compile(r"^/api/health"), "System"),
|
||||
(re.compile(r"^/api/config"), "System"),
|
||||
(re.compile(r"^/api/diagnostics"), "System"),
|
||||
(re.compile(r"^/api/dashboard"), "System"),
|
||||
(re.compile(r"^/api/events"), "System"),
|
||||
]
|
||||
|
||||
|
||||
def tag_for_path(path: str) -> str:
|
||||
"""Return the documentation tag for an API path (``Frontend`` as fallback)."""
|
||||
for pattern, tag in _TAG_RULES:
|
||||
if pattern.match(path):
|
||||
return tag
|
||||
return "Frontend"
|
||||
|
||||
|
||||
# Normalise tags declared by individual routers (``tags=["auth"]``) to the
|
||||
# canonical, documented tag names.
|
||||
_TAG_ALIASES: dict[str, str] = {
|
||||
"auth": "Auth",
|
||||
"admin": "Admin",
|
||||
"plugins": "Plugins",
|
||||
"push": "Push",
|
||||
"files": "Files",
|
||||
"vaults": "Vaults",
|
||||
"search": "Search",
|
||||
"backups": "Backups",
|
||||
"export": "Export",
|
||||
"sharing": "Sharing",
|
||||
"webhooks": "Webhooks",
|
||||
"conflicts": "Conflicts",
|
||||
"system": "System",
|
||||
"frontend": "Frontend",
|
||||
"ai": "AI",
|
||||
"bookslm": "BooksLM",
|
||||
"mcp": "MCP",
|
||||
"pdf": "PDF",
|
||||
"bookmarks": "Bookmarks",
|
||||
}
|
||||
|
||||
_CANONICAL_TAGS = {tag["name"] for tag in TAGS_METADATA}
|
||||
|
||||
|
||||
def canonical_tag(name: str) -> str:
|
||||
"""Map a router-declared tag to its documented name (unchanged if unknown)."""
|
||||
mapped = _TAG_ALIASES.get(name.lower(), name)
|
||||
return mapped if mapped in _CANONICAL_TAGS else name
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Request/response examples injected into the schema
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_ENDPOINT_EXAMPLES: dict[tuple[str, str], dict[str, Any]] = {
|
||||
("post", "/api/file/{vault_name}"): {
|
||||
"request": {"path": "notes/Nouvelle note.md", "content": "# Titre\n\nContenu"},
|
||||
"response": {"success": True, "path": "notes/Nouvelle note.md"},
|
||||
},
|
||||
("put", "/api/file/{vault_name}/save"): {
|
||||
"request": {"path": "notes/Accueil.md", "content": "# Accueil\n\nMis à jour."},
|
||||
"response": {"status": "ok", "vault": "TestVault", "path": "notes/Accueil.md", "size": 26},
|
||||
},
|
||||
("post", "/api/search/replace"): {
|
||||
"request": {"query": "Python", "replacement": "Python 3", "vault": "all", "dry_run": True},
|
||||
"response": {"matches": [{"vault": "TestVault", "path": "note1.md", "title": "Python", "match_count": 3}], "total_matches": 3, "dry_run": True},
|
||||
},
|
||||
("post", "/api/ai/improve"): {
|
||||
"request": {"text": "ce texte est bof", "provider": "deepseek"},
|
||||
"response": {"result": "Ce texte mérite d'être amélioré.", "provider": "deepseek"},
|
||||
},
|
||||
("post", "/api/ai/bookslm/chat"): {
|
||||
"request": {"vault": "TestVault", "directory": "projets", "message": "Résume ce dossier", "conversation_history": []},
|
||||
"response": {"token": "Voici un résumé…", "provider": "deepseek", "model": "deepseek-chat"},
|
||||
},
|
||||
("post", "/api/share/{vault_name}"): {
|
||||
"request": {"path": "notes/Accueil.md", "expires_in_hours": 168},
|
||||
"response": {"id": "abc…", "token": "abc…", "vault": "TestVault", "path": "notes/Accueil.md", "url": "/s/abc…"},
|
||||
},
|
||||
("post", "/api/webhooks"): {
|
||||
"request": {"name": "CI", "url": "https://example.com/hook", "events": ["file_modified"], "secret": "s3cr3t"},
|
||||
"response": {"id": "3f2c…", "name": "CI", "url": "https://example.com/hook", "events": ["file_modified"], "enabled": True},
|
||||
},
|
||||
}
|
||||
|
||||
# Common error responses documented on every operation.
|
||||
_COMMON_ERRORS: dict[str, dict[str, Any]] = {
|
||||
"401": {
|
||||
"description": "Authentication required or token expired",
|
||||
"content": {"application/json": {"example": {"detail": "Not authenticated"}}},
|
||||
},
|
||||
"403": {
|
||||
"description": "Access to the vault is denied",
|
||||
"content": {"application/json": {"example": {"detail": "Accès refusé à la vault 'Notes'"}}},
|
||||
},
|
||||
"404": {
|
||||
"description": "Resource not found",
|
||||
"content": {"application/json": {"example": {"detail": "File not found: notes/x.md"}}},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation error",
|
||||
"content": {"application/json": {"example": {"detail": [{"loc": ["query", "path"], "msg": "field required", "type": "value_error.missing"}]}}},
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal server error",
|
||||
"content": {"application/json": {"example": {"detail": "Internal server error"}}},
|
||||
},
|
||||
}
|
||||
|
||||
# Paths that return a binary/streaming body — never add a JSON error example
|
||||
# to their 200 response, and skip the 500 JSON example for streams.
|
||||
_BINARY_MEDIA = ("application/pdf", "application/octet-stream", "image/", "text/event-stream")
|
||||
|
||||
|
||||
def _is_binary_operation(operation: dict[str, Any]) -> bool:
|
||||
content = operation.get("responses", {}).get("200", {}).get("content", {})
|
||||
return any(media.startswith(_BINARY_MEDIA) for media in content)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# MCP endpoint (not a FastAPI route: custom ASGI mount) — documented manually
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_MCP_DESCRIPTION = (
|
||||
"**Model Context Protocol** server over Streamable HTTP (JSON-RPC 2.0). "
|
||||
"Exposes the shared AI tool layer to external MCP clients (Claude Desktop, "
|
||||
"Cursor…). Authentication uses `Authorization: Bearer <JWT>` (the same "
|
||||
"token as the REST API).\n\n"
|
||||
"Primitives: read/search **tools** directly; write/destructive tools as a "
|
||||
"two-step `propose_<tool>` / `apply_<tool>` pair (signed, single-use "
|
||||
"confirmation token); **resources** `vault://<name>` and "
|
||||
"`vault://<name>/<path>` (read-only, secrets redacted); **prompts** "
|
||||
"`summarize-directory`, `generate-note`, `find-related`.\n\n"
|
||||
"See `docs/MCP_GUIDE.md` for client setup."
|
||||
)
|
||||
|
||||
|
||||
def _inject_mcp_path(schema: dict[str, Any]) -> None:
|
||||
"""Add the MCP Streamable HTTP endpoint to the schema (idempotent)."""
|
||||
paths = schema.setdefault("paths", {})
|
||||
if "/mcp" in paths:
|
||||
return
|
||||
paths["/mcp"] = {
|
||||
"post": {
|
||||
"tags": ["MCP"],
|
||||
"summary": "MCP Streamable HTTP endpoint (JSON-RPC 2.0)",
|
||||
"operationId": "mcp_streamable_http",
|
||||
"description": _MCP_DESCRIPTION,
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"example": {
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "tools/list",
|
||||
"params": {},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "JSON-RPC response (or 202 for notifications)",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"example": {
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"result": {"tools": []},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
"security": [{"bearerAuth": []}],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def enrich_openapi_schema(schema: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Enrich a FastAPI-generated OpenAPI schema in place and return it.
|
||||
|
||||
The transformation is idempotent: calling it twice yields the same result.
|
||||
"""
|
||||
schema.setdefault("openapi", "3.1.0")
|
||||
info = schema.setdefault("info", {})
|
||||
info.setdefault("description", API_DESCRIPTION.strip())
|
||||
info.setdefault("contact", {"name": "ObsiGate", "url": "https://git.dracodev.net/Projets/ObsiGate"})
|
||||
info.setdefault("license", {"name": "MIT"})
|
||||
|
||||
schema.setdefault("servers", [{"url": "/", "description": "Current ObsiGate instance"}])
|
||||
schema["externalDocs"] = {
|
||||
"description": "ObsiGate source repository",
|
||||
"url": "https://git.dracodev.net/Projets/ObsiGate",
|
||||
}
|
||||
schema["tags"] = TAGS_METADATA
|
||||
|
||||
_inject_mcp_path(schema)
|
||||
|
||||
components = schema.setdefault("components", {})
|
||||
security_schemes = components.setdefault("securitySchemes", {})
|
||||
security_schemes.setdefault("bearerAuth", {
|
||||
"type": "http",
|
||||
"scheme": "bearer",
|
||||
"bearerFormat": "JWT",
|
||||
"description": "JWT access token issued by POST /api/auth/login.",
|
||||
})
|
||||
security_schemes.setdefault("cookieAuth", {
|
||||
"type": "apiKey",
|
||||
"in": "cookie",
|
||||
"name": "obsigate_token",
|
||||
"description": "HTTP-only session cookie (browser clients).",
|
||||
})
|
||||
components.setdefault("schemas", {}).setdefault("ErrorResponse", {
|
||||
"type": "object",
|
||||
"properties": {"detail": {"title": "Detail", "description": "Error message"}},
|
||||
})
|
||||
|
||||
for path, operations in schema.get("paths", {}).items():
|
||||
default_tag = tag_for_path(path)
|
||||
for method, operation in operations.items():
|
||||
if not isinstance(operation, dict):
|
||||
continue
|
||||
if operation.get("tags"):
|
||||
operation["tags"] = [canonical_tag(t) for t in operation["tags"]]
|
||||
else:
|
||||
operation["tags"] = [default_tag]
|
||||
|
||||
# Document common error responses without overriding explicit ones.
|
||||
responses = operation.setdefault("responses", {})
|
||||
binary = _is_binary_operation(operation)
|
||||
for code, spec in _COMMON_ERRORS.items():
|
||||
if code == "500" and binary:
|
||||
continue
|
||||
responses.setdefault(code, spec)
|
||||
|
||||
# Inject request/response examples for key endpoints.
|
||||
example = _ENDPOINT_EXAMPLES.get((method, path))
|
||||
if example:
|
||||
if "request" in example:
|
||||
body = operation.setdefault("requestBody", {})
|
||||
content = body.setdefault("content", {}).setdefault("application/json", {})
|
||||
content.setdefault("example", example["request"])
|
||||
if "response" in example:
|
||||
ok = responses.setdefault("200", {})
|
||||
content = ok.setdefault("content", {}).setdefault("application/json", {})
|
||||
content.setdefault("example", example["response"])
|
||||
|
||||
return schema
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# /api landing page
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_METHOD_COLORS = {
|
||||
"get": "#2563eb",
|
||||
"post": "#16a34a",
|
||||
"put": "#d97706",
|
||||
"patch": "#7c3aed",
|
||||
"delete": "#dc2626",
|
||||
"head": "#64748b",
|
||||
"options": "#64748b",
|
||||
}
|
||||
|
||||
|
||||
def render_api_landing(version: str = "") -> str:
|
||||
"""Return the self-contained HTML for the ``/api`` documentation page."""
|
||||
return f"""<!DOCTYPE html>
|
||||
<html lang="en" data-theme="dark">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>ObsiGate API{(' — v' + version) if version else ''}</title>
|
||||
<style>
|
||||
:root {{
|
||||
--bg: #0f172a; --surface: #1e293b; --border: #334155;
|
||||
--text: #e2e8f0; --muted: #94a3b8; --accent: #60a5fa; --accent-2: #34d399;
|
||||
}}
|
||||
html[data-theme="light"] {{
|
||||
--bg: #f8fafc; --surface: #ffffff; --border: #e2e8f0;
|
||||
--text: #0f172a; --muted: #64748b; --accent: #2563eb; --accent-2: #059669;
|
||||
}}
|
||||
* {{ box-sizing: border-box; }}
|
||||
body {{
|
||||
margin: 0; background: var(--bg); color: var(--text);
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
line-height: 1.5;
|
||||
}}
|
||||
.wrap {{ max-width: 1080px; margin: 0 auto; padding: 40px 24px 80px; }}
|
||||
header h1 {{ margin: 0 0 4px; font-size: 1.9rem; }}
|
||||
header p {{ margin: 0; color: var(--muted); }}
|
||||
.actions {{ display: flex; flex-wrap: wrap; gap: 10px; margin: 24px 0 32px; }}
|
||||
.actions a {{
|
||||
text-decoration: none; color: var(--text); background: var(--surface);
|
||||
border: 1px solid var(--border); border-radius: 8px; padding: 10px 16px;
|
||||
font-weight: 600; font-size: .9rem; transition: border-color .15s, transform .15s;
|
||||
}}
|
||||
.actions a:hover {{ border-color: var(--accent); transform: translateY(-1px); }}
|
||||
.actions a.primary {{ background: var(--accent); border-color: var(--accent); color: #fff; }}
|
||||
.meta {{ color: var(--muted); font-size: .85rem; margin-bottom: 24px; }}
|
||||
.tag {{ margin: 28px 0; }}
|
||||
.tag > h2 {{
|
||||
font-size: 1.1rem; margin: 0 0 2px; display: flex; align-items: center; gap: 8px;
|
||||
}}
|
||||
.tag > p {{ margin: 0 0 12px; color: var(--muted); font-size: .85rem; }}
|
||||
.ops {{ display: flex; flex-direction: column; gap: 6px; }}
|
||||
.op {{
|
||||
display: flex; align-items: center; gap: 12px; padding: 8px 12px;
|
||||
background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
|
||||
text-decoration: none; color: var(--text); font-size: .88rem;
|
||||
}}
|
||||
.op:hover {{ border-color: var(--accent); }}
|
||||
.method {{
|
||||
flex: 0 0 62px; text-align: center; font-weight: 700; font-size: .68rem;
|
||||
text-transform: uppercase; letter-spacing: .04em; color: #fff; border-radius: 5px; padding: 3px 0;
|
||||
}}
|
||||
.op code {{ color: var(--accent-2); font-size: .85rem; }}
|
||||
.op .summary {{ color: var(--muted); margin-left: auto; text-align: right; }}
|
||||
.loading {{ color: var(--muted); }}
|
||||
footer {{ margin-top: 48px; color: var(--muted); font-size: .8rem; }}
|
||||
@media (max-width: 640px) {{ .op .summary {{ display: none; }} }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<header>
|
||||
<h1>ObsiGate API</h1>
|
||||
<p>Interactive reference for the ObsiGate REST API (OpenAPI 3.1).</p>
|
||||
</header>
|
||||
<div class="actions">
|
||||
<a class="primary" href="/docs">Swagger UI — try it</a>
|
||||
<a href="/redoc">ReDoc — reference</a>
|
||||
<a href="/openapi.json">OpenAPI JSON</a>
|
||||
<a href="/">← Back to ObsiGate</a>
|
||||
</div>
|
||||
<div class="meta" id="meta">Loading specification…</div>
|
||||
<div id="tags"><p class="loading">Loading endpoints…</p></div>
|
||||
<footer>Generated from the live OpenAPI schema. Authentication: <code>Authorization: Bearer <token></code>.</footer>
|
||||
</div>
|
||||
<script>
|
||||
(function () {{
|
||||
var COLORS = {_js_colors()};
|
||||
function el(tag, cls, text) {{
|
||||
var e = document.createElement(tag);
|
||||
if (cls) e.className = cls;
|
||||
if (text != null) e.textContent = text;
|
||||
return e;
|
||||
}}
|
||||
fetch('/openapi.json', {{ credentials: 'include' }})
|
||||
.then(function (r) {{ if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); }})
|
||||
.then(function (spec) {{
|
||||
var info = spec.info || {{}};
|
||||
var tagMeta = {{}};
|
||||
(spec.tags || []).forEach(function (t) {{ tagMeta[t.name] = t.description || ''; }});
|
||||
var groups = {{}};
|
||||
var paths = spec.paths || {{}};
|
||||
var opCount = 0;
|
||||
Object.keys(paths).forEach(function (path) {{
|
||||
var ops = paths[path];
|
||||
Object.keys(ops).forEach(function (method) {{
|
||||
if (['get','post','put','patch','delete','head','options'].indexOf(method) === -1) return;
|
||||
var op = ops[method];
|
||||
if (op.deprecated && false) return;
|
||||
opCount++;
|
||||
var tag = (op.tags && op.tags[0]) || 'Other';
|
||||
(groups[tag] = groups[tag] || []).push({{ path: path, method: method, summary: op.summary || '' }});
|
||||
}});
|
||||
}});
|
||||
document.getElementById('meta').textContent =
|
||||
(info.title || 'ObsiGate') + (info.version ? ' v' + info.version : '') +
|
||||
' — OpenAPI ' + (spec.openapi || '3.1') + ' — ' + opCount + ' operations';
|
||||
var host = document.getElementById('tags');
|
||||
host.innerHTML = '';
|
||||
Object.keys(groups).forEach(function (tag) {{
|
||||
var section = el('section', 'tag');
|
||||
section.appendChild(el('h2', null, tag));
|
||||
if (tagMeta[tag]) section.appendChild(el('p', null, tagMeta[tag]));
|
||||
var ops = el('div', 'ops');
|
||||
groups[tag].forEach(function (o) {{
|
||||
var a = el('a', 'op');
|
||||
a.href = '/docs#/' + encodeURIComponent(tag) + '/' + encodeURIComponent(o.path);
|
||||
var m = el('span', 'method', o.method);
|
||||
m.style.background = COLORS[o.method] || '#64748b';
|
||||
a.appendChild(m);
|
||||
a.appendChild(el('code', null, o.path));
|
||||
if (o.summary) a.appendChild(el('span', 'summary', o.summary));
|
||||
ops.appendChild(a);
|
||||
}});
|
||||
section.appendChild(ops);
|
||||
host.appendChild(section);
|
||||
}});
|
||||
}})
|
||||
.catch(function (e) {{
|
||||
document.getElementById('meta').textContent = 'Failed to load OpenAPI schema: ' + e.message;
|
||||
}});
|
||||
}})();
|
||||
</script>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
|
||||
def _js_colors() -> str:
|
||||
"""Serialise the method→colour map for the landing page script."""
|
||||
import json
|
||||
|
||||
return json.dumps(_METHOD_COLORS)
|
||||
@@ -6,14 +6,13 @@ Used by both the authenticated API and public share views.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from weasyprint import HTML
|
||||
|
||||
logger = logging.getLogger("obsigate.pdf")
|
||||
|
||||
|
||||
def generate_pdf(html_content: str, title: str = "document", base_url: Optional[str] = None) -> bytes:
|
||||
def generate_pdf(html_content: str, title: str = "document", base_url: str | None = None) -> bytes:
|
||||
"""Generate a PDF from HTML content.
|
||||
|
||||
Args:
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
"""PDF text extraction for ObsiGate — pypdf backend, pymupdf optional fallback."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from concurrent.futures import TimeoutError as FuturesTimeout
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Configurable limits (see ROADMAP #74 G3):
|
||||
# OBSIGATE_PDF_MAX_SIZE_MB — PDFs larger than this are not text-extracted (default 50)
|
||||
# OBSIGATE_PDF_EXTRACT_TIMEOUT — seconds before extraction is abandoned (default 30)
|
||||
PDF_MAX_SIZE_MB: int = int(os.environ.get("OBSIGATE_PDF_MAX_SIZE_MB", "50"))
|
||||
PDF_EXTRACT_TIMEOUT: float = float(os.environ.get("OBSIGATE_PDF_EXTRACT_TIMEOUT", "30"))
|
||||
|
||||
PDF_READER: str = "pypdf"
|
||||
PdfReader: Any = None
|
||||
try:
|
||||
import fitz # pymupdf
|
||||
PDF_READER = "pymupdf"
|
||||
logger.info("PDF reader: pymupdf (high performance)")
|
||||
except ImportError:
|
||||
try:
|
||||
from pypdf import PdfReader # type: ignore
|
||||
logger.info("PDF reader: pypdf (pure Python)")
|
||||
except ImportError:
|
||||
logger.warning("No PDF reader available — install pypdf or pymupdf")
|
||||
|
||||
|
||||
def pdf_exceeds_size_limit(file_path: Path) -> bool:
|
||||
"""True if the PDF is larger than OBSIGATE_PDF_MAX_SIZE_MB (skip text extraction)."""
|
||||
try:
|
||||
return file_path.stat().st_size > PDF_MAX_SIZE_MB * 1024 * 1024
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _run_with_timeout(fn, *args, timeout: float):
|
||||
"""Run a sync function in a worker thread with a hard timeout.
|
||||
|
||||
A timed-out extraction leaves its worker thread running (daemon-style pool
|
||||
is abandoned), but the request itself is freed — acceptable trade-off for
|
||||
pathological PDFs on a self-hosted single-user server.
|
||||
"""
|
||||
executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="pdf-extract")
|
||||
try:
|
||||
future = executor.submit(fn, *args)
|
||||
return future.result(timeout=timeout)
|
||||
finally:
|
||||
executor.shutdown(wait=False)
|
||||
|
||||
|
||||
def extract_pdf_text(file_path: Path, max_chars: int = 100000) -> str:
|
||||
"""Extract text from a PDF file. Returns empty string on failure.
|
||||
|
||||
Oversized PDFs (> OBSIGATE_PDF_MAX_SIZE_MB) and extractions exceeding
|
||||
OBSIGATE_PDF_EXTRACT_TIMEOUT seconds return "" instead of blocking.
|
||||
"""
|
||||
if PdfReader is None and PDF_READER == "pypdf":
|
||||
return ""
|
||||
if pdf_exceeds_size_limit(file_path):
|
||||
logger.info("PDF too large to index (> %d MB), skipping text extraction: %s",
|
||||
PDF_MAX_SIZE_MB, file_path)
|
||||
return ""
|
||||
try:
|
||||
if PDF_READER == "pymupdf":
|
||||
return _run_with_timeout(_extract_pymupdf, file_path, max_chars,
|
||||
timeout=PDF_EXTRACT_TIMEOUT)
|
||||
else:
|
||||
return _run_with_timeout(_extract_pypdf, file_path, max_chars,
|
||||
timeout=PDF_EXTRACT_TIMEOUT)
|
||||
except FuturesTimeout:
|
||||
logger.warning("PDF text extraction timed out (%ss): %s", PDF_EXTRACT_TIMEOUT, file_path)
|
||||
return ""
|
||||
except Exception as e:
|
||||
logger.warning("Failed to extract PDF text from %s: %s", file_path, e)
|
||||
return ""
|
||||
|
||||
|
||||
def extract_pdf_metadata(file_path: Path) -> dict:
|
||||
"""Extract metadata from a PDF file."""
|
||||
info = {"pages": 0, "title": "", "author": ""}
|
||||
try:
|
||||
if PDF_READER == "pymupdf":
|
||||
doc = fitz.open(str(file_path))
|
||||
info["pages"] = doc.page_count
|
||||
meta = doc.metadata or {}
|
||||
info["title"] = meta.get("title", "")
|
||||
info["author"] = meta.get("author", "")
|
||||
doc.close()
|
||||
elif PdfReader is not None:
|
||||
reader = PdfReader(str(file_path))
|
||||
info["pages"] = len(reader.pages)
|
||||
meta = reader.metadata or {}
|
||||
if meta:
|
||||
info["title"] = str(meta.get("/Title", ""))
|
||||
info["author"] = str(meta.get("/Author", ""))
|
||||
except Exception as e:
|
||||
logger.warning("Failed to extract PDF metadata from %s: %s", file_path, e)
|
||||
return info
|
||||
|
||||
|
||||
def extract_pdf_toc(file_path: Path) -> list[dict]:
|
||||
"""Extract table of contents (bookmarks/outline) from a PDF file.
|
||||
|
||||
Returns a list of {title, page, level} dicts, or empty list on failure.
|
||||
"""
|
||||
toc: list[dict] = []
|
||||
try:
|
||||
if PDF_READER == "pymupdf":
|
||||
doc = fitz.open(str(file_path))
|
||||
raw = doc.get_toc(simple=False)
|
||||
for item in raw:
|
||||
toc.append({
|
||||
"title": str(item[1]),
|
||||
"page": item[2],
|
||||
"level": item[0],
|
||||
})
|
||||
doc.close()
|
||||
elif PdfReader is not None:
|
||||
reader = PdfReader(str(file_path))
|
||||
outline = reader.outline
|
||||
if outline:
|
||||
def _flatten(items, level=1):
|
||||
for item in items:
|
||||
if isinstance(item, list):
|
||||
_flatten(item, level + 1)
|
||||
elif hasattr(item, 'title') and hasattr(item, 'page'):
|
||||
page_num = reader.get_page_number(item.page) + 1 if hasattr(item, 'page') and item.page else 1
|
||||
toc.append({
|
||||
"title": str(item.title),
|
||||
"page": page_num,
|
||||
"level": level,
|
||||
})
|
||||
_flatten(outline)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to extract PDF TOC from %s: %s", file_path, e)
|
||||
return toc
|
||||
|
||||
|
||||
def _extract_pymupdf(file_path: Path, max_chars: int) -> str:
|
||||
doc = fitz.open(str(file_path))
|
||||
parts = []
|
||||
total = 0
|
||||
for page in doc:
|
||||
text = page.get_text()
|
||||
if text:
|
||||
if total + len(text) > max_chars:
|
||||
remaining = max_chars - total
|
||||
if remaining > 0:
|
||||
parts.append(text[:remaining])
|
||||
break
|
||||
parts.append(text)
|
||||
total += len(text)
|
||||
doc.close()
|
||||
return "\f".join(parts)
|
||||
|
||||
|
||||
def _extract_pypdf(file_path: Path, max_chars: int) -> str:
|
||||
if PdfReader is None:
|
||||
return ""
|
||||
reader = PdfReader(str(file_path))
|
||||
parts = []
|
||||
total = 0
|
||||
for page in reader.pages:
|
||||
text = page.extract_text() or ""
|
||||
if text:
|
||||
if total + len(text) > max_chars:
|
||||
remaining = max_chars - total
|
||||
if remaining > 0:
|
||||
parts.append(text[:remaining])
|
||||
break
|
||||
parts.append(text)
|
||||
total += len(text)
|
||||
return "\f".join(parts)
|
||||
@@ -0,0 +1,624 @@
|
||||
"""Plugin system for ObsiGate — backend API and manifest validation.
|
||||
|
||||
Plugins extend ObsiGate with custom renderers, search filters, and editor
|
||||
actions. They run sandboxed in a Web Worker on the frontend; the backend
|
||||
handles manifest validation, storage, and lifecycle (install / uninstall /
|
||||
enable / disable), all scoped to a single vault.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import time as _time
|
||||
import zipfile
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, ClassVar
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from backend.auth.middleware import check_vault_access, require_admin, require_auth
|
||||
from backend.indexer import get_vault_data
|
||||
|
||||
logger = logging.getLogger("obsigate.plugins")
|
||||
router = APIRouter(prefix="/api/plugins", tags=["plugins"])
|
||||
|
||||
# ── Constants ──────────────────────────────────────────────────────────────
|
||||
PLUGINS_DIR_NAME = ".obsigate-plugins"
|
||||
MANIFEST_FILENAME = "plugin.json"
|
||||
MAX_PLUGIN_SIZE = 500_000 # 500 KB per plugin file
|
||||
MAX_PLUGINS_PER_VAULT = 50
|
||||
MAX_PLUGIN_FILES = 100 # max files in an installed plugin zip
|
||||
|
||||
ALLOWED_HOOKS = frozenset([
|
||||
"onFileRender",
|
||||
"onSearchFilter",
|
||||
"onEditorAction",
|
||||
"onSidebarItem",
|
||||
"onFileCreate",
|
||||
"onFileDelete",
|
||||
"onVaultMount",
|
||||
])
|
||||
|
||||
ALLOWED_PERMISSIONS = frozenset([
|
||||
"read_files",
|
||||
"write_files",
|
||||
"read_vault_metadata",
|
||||
"network_request",
|
||||
"ui_notify",
|
||||
"access_clipboard",
|
||||
])
|
||||
|
||||
|
||||
# ── Manifest ───────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginManifest:
|
||||
"""Validated plugin manifest from plugin.json."""
|
||||
|
||||
name: str
|
||||
version: str
|
||||
description: str
|
||||
author: str
|
||||
main: str
|
||||
hooks: dict[str, str] = field(default_factory=dict)
|
||||
permissions: list[str] = field(default_factory=list)
|
||||
min_obsigate_version: str = "2.1.0"
|
||||
homepage: str | None = None
|
||||
repository: str | None = None
|
||||
license: str = "MIT"
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> "PluginManifest":
|
||||
"""Validate and build a manifest from a raw dict (JSON object)."""
|
||||
required = ["name", "version", "description", "author", "main"]
|
||||
for field_name in required:
|
||||
if field_name not in data or not data[field_name]:
|
||||
raise ValueError(f"Missing required field: {field_name}")
|
||||
|
||||
name = data["name"]
|
||||
if not re.fullmatch(r"[a-z0-9]([a-z0-9-]*[a-z0-9])?", name):
|
||||
raise ValueError(
|
||||
"Plugin name must be lowercase alphanumeric with hyphens (e.g., 'my-plugin')"
|
||||
)
|
||||
|
||||
version = data["version"]
|
||||
if not re.fullmatch(r"\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?", version):
|
||||
raise ValueError("Version must be semantic version (e.g., '1.0.0')")
|
||||
|
||||
hooks = data.get("hooks", {})
|
||||
if not isinstance(hooks, dict):
|
||||
raise TypeError("'hooks' must be an object mapping hook name to handler name")
|
||||
for hook_name, handler in hooks.items():
|
||||
if hook_name not in ALLOWED_HOOKS:
|
||||
raise ValueError(
|
||||
f"Unknown hook: {hook_name}. Allowed: {sorted(ALLOWED_HOOKS)}"
|
||||
)
|
||||
if not isinstance(handler, str) or not handler:
|
||||
raise ValueError(f"Hook handler for '{hook_name}' must be a non-empty string")
|
||||
|
||||
permissions = data.get("permissions", [])
|
||||
if not isinstance(permissions, list):
|
||||
raise TypeError("'permissions' must be a list of permission names")
|
||||
for perm in permissions:
|
||||
if perm not in ALLOWED_PERMISSIONS:
|
||||
raise ValueError(
|
||||
f"Unknown permission: {perm}. Allowed: {sorted(ALLOWED_PERMISSIONS)}"
|
||||
)
|
||||
|
||||
return cls(
|
||||
name=name,
|
||||
version=version,
|
||||
description=data["description"],
|
||||
author=data["author"],
|
||||
main=data["main"],
|
||||
hooks=hooks,
|
||||
permissions=permissions,
|
||||
min_obsigate_version=data.get("min_obsigate_version", "2.1.0"),
|
||||
homepage=data.get("homepage"),
|
||||
repository=data.get("repository"),
|
||||
license=data.get("license", "MIT"),
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"version": self.version,
|
||||
"description": self.description,
|
||||
"author": self.author,
|
||||
"main": self.main,
|
||||
"hooks": self.hooks,
|
||||
"permissions": self.permissions,
|
||||
"min_obsigate_version": self.min_obsigate_version,
|
||||
"homepage": self.homepage,
|
||||
"repository": self.repository,
|
||||
"license": self.license,
|
||||
}
|
||||
|
||||
|
||||
# ── Storage path helpers ───────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _vault_plugins_dir(vault_name: str) -> Path:
|
||||
"""Return the plugins directory for a vault (creating it if absent)."""
|
||||
vault_data = get_vault_data(vault_name)
|
||||
if not vault_data:
|
||||
raise HTTPException(status_code=404, detail=f"Vault '{vault_name}' not found")
|
||||
base = Path(vault_data["path"])
|
||||
pd = base / PLUGINS_DIR_NAME
|
||||
pd.mkdir(parents=True, exist_ok=True)
|
||||
return pd
|
||||
|
||||
|
||||
# ── Manager ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class PluginManager:
|
||||
"""Manages plugin lifecycle within a single plugins directory."""
|
||||
|
||||
def __init__(self, plugins_dir):
|
||||
self.plugins_dir = Path(plugins_dir)
|
||||
self.plugins_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _plugin_dir(self, name: str) -> Path:
|
||||
return self.plugins_dir / name
|
||||
|
||||
# ---- install / uninstall ----
|
||||
|
||||
def install(self, manifest: dict, code: str) -> dict[str, Any]:
|
||||
"""Install a plugin from a manifest dict + entry point source string."""
|
||||
m = PluginManifest.from_dict(manifest)
|
||||
|
||||
plugin_dir = self._plugin_dir(m.name)
|
||||
if plugin_dir.exists():
|
||||
raise ValueError(f"Plugin '{m.name}' is already installed")
|
||||
|
||||
installed = list(self.plugins_dir.iterdir()) if self.plugins_dir.exists() else []
|
||||
if installed and len(installed) >= MAX_PLUGINS_PER_VAULT:
|
||||
raise ValueError(f"Maximum of {MAX_PLUGINS_PER_VAULT} plugins per vault reached")
|
||||
|
||||
if len(code.encode("utf-8")) > MAX_PLUGIN_SIZE:
|
||||
raise ValueError(f"Plugin file exceeds {MAX_PLUGIN_SIZE} bytes")
|
||||
|
||||
plugin_dir.mkdir(parents=True, exist_ok=True)
|
||||
(plugin_dir / MANIFEST_FILENAME).write_text(
|
||||
json.dumps(m.to_dict(), indent=2), encoding="utf-8"
|
||||
)
|
||||
(plugin_dir / m.main).write_text(code, encoding="utf-8")
|
||||
logger.info("Installed plugin '%s' v%s", m.name, m.version)
|
||||
return {
|
||||
"name": m.name,
|
||||
"version": m.version,
|
||||
"description": m.description,
|
||||
"author": m.author,
|
||||
"main": m.main,
|
||||
"enabled": True,
|
||||
}
|
||||
|
||||
def uninstall(self, name: str) -> None:
|
||||
plugin_dir = self._plugin_dir(name)
|
||||
if not plugin_dir.exists():
|
||||
raise ValueError(f"Plugin '{name}' not found")
|
||||
import shutil
|
||||
|
||||
shutil.rmtree(plugin_dir)
|
||||
logger.info("Uninstalled plugin '%s'", name)
|
||||
|
||||
# ---- enable / disable ----
|
||||
|
||||
def enable(self, name: str) -> None:
|
||||
plugin_dir = self._plugin_dir(name)
|
||||
if not plugin_dir.exists():
|
||||
raise ValueError(f"Plugin '{name}' not found")
|
||||
marker = plugin_dir / ".disabled"
|
||||
if marker.exists():
|
||||
marker.unlink()
|
||||
|
||||
def disable(self, name: str) -> None:
|
||||
plugin_dir = self._plugin_dir(name)
|
||||
if not plugin_dir.exists():
|
||||
raise ValueError(f"Plugin '{name}' not found")
|
||||
(plugin_dir / ".disabled").write_text("", encoding="utf-8")
|
||||
|
||||
def is_disabled(self, name: str) -> bool:
|
||||
return (self._plugin_dir(name) / ".disabled").exists()
|
||||
|
||||
# ---- listing / metadata ----
|
||||
|
||||
def list_plugins(self) -> list[dict[str, Any]]:
|
||||
if not self.plugins_dir.exists():
|
||||
return []
|
||||
out = []
|
||||
for d in sorted(self.plugins_dir.iterdir()):
|
||||
if not d.is_dir():
|
||||
continue
|
||||
manifest_file = d / MANIFEST_FILENAME
|
||||
if not manifest_file.exists():
|
||||
continue
|
||||
try:
|
||||
m = PluginManifest.from_dict(json.loads(manifest_file.read_text(encoding="utf-8")))
|
||||
except (json.JSONDecodeError, ValueError) as exc:
|
||||
logger.warning("Invalid plugin dir '%s': %s", d.name, exc)
|
||||
continue
|
||||
out.append({
|
||||
"name": m.name,
|
||||
"version": m.version,
|
||||
"description": m.description,
|
||||
"author": m.author,
|
||||
"main": m.main,
|
||||
"enabled": not self.is_disabled(m.name),
|
||||
"hooks": m.hooks,
|
||||
"permissions": m.permissions,
|
||||
})
|
||||
return out
|
||||
|
||||
def get_plugin(self, name: str) -> dict[str, Any] | None:
|
||||
plugin_dir = self._plugin_dir(name)
|
||||
manifest_file = plugin_dir / MANIFEST_FILENAME
|
||||
if not manifest_file.exists():
|
||||
return None
|
||||
try:
|
||||
m = PluginManifest.from_dict(json.loads(manifest_file.read_text(encoding="utf-8")))
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
return None
|
||||
return {
|
||||
"manifest": m.to_dict(),
|
||||
"name": m.name,
|
||||
"version": m.version,
|
||||
"enabled": not self.is_disabled(m.name),
|
||||
}
|
||||
|
||||
def get_plugin_code(self, name: str, file_name: str) -> str:
|
||||
plugin_dir = self._plugin_dir(name)
|
||||
manifest_file = plugin_dir / MANIFEST_FILENAME
|
||||
if not manifest_file.exists():
|
||||
raise ValueError(f"Plugin '{name}' not found")
|
||||
target = plugin_dir / file_name
|
||||
# Prevent path traversal
|
||||
try:
|
||||
target.resolve().relative_to(plugin_dir.resolve())
|
||||
except (ValueError, OSError):
|
||||
raise ValueError(f"Invalid file path: {file_name}")
|
||||
if not target.is_file():
|
||||
raise ValueError(f"File '{file_name}' not found for plugin '{name}'")
|
||||
return target.read_text(encoding="utf-8")
|
||||
|
||||
def get_hooks(self, name: str) -> dict[str, str]:
|
||||
plugin = self.get_plugin(name)
|
||||
if not plugin:
|
||||
raise ValueError(f"Plugin '{name}' not found")
|
||||
return plugin["manifest"].get("hooks", {})
|
||||
|
||||
|
||||
class PluginRegistry:
|
||||
"""Aggregates enabled plugins for a plugins directory."""
|
||||
|
||||
def get_enabled_plugins(self, plugins_dir) -> list[dict[str, Any]]:
|
||||
manager = PluginManager(plugins_dir)
|
||||
return [p for p in manager.list_plugins() if p["enabled"]]
|
||||
|
||||
def get_plugins_by_hook(self, plugins_dir, hook: str) -> list[dict[str, Any]]:
|
||||
manager = PluginManager(plugins_dir)
|
||||
return [
|
||||
p for p in manager.list_plugins()
|
||||
if p["enabled"] and hook in p.get("hooks", {})
|
||||
]
|
||||
|
||||
def scan_vault(self, vault_name: str, vault_path) -> list[dict[str, Any]]:
|
||||
"""Scan a vault directory for installed plugins (startup discovery).
|
||||
|
||||
Returns the list of installed-but-valid plugins so the app can log
|
||||
how many are available per vault. Invalid/zombie plugin dirs are
|
||||
skipped gracefully.
|
||||
"""
|
||||
pd = Path(vault_path) / PLUGINS_DIR_NAME
|
||||
if not pd.is_dir():
|
||||
return []
|
||||
return PluginManager(pd).list_plugins()
|
||||
|
||||
|
||||
# Module-level registry singleton (startup scan + runtime lookups)
|
||||
_registry_singleton: PluginRegistry | None = None
|
||||
|
||||
|
||||
def get_plugin_registry() -> PluginRegistry:
|
||||
"""Return the shared PluginRegistry instance."""
|
||||
global _registry_singleton
|
||||
if _registry_singleton is None:
|
||||
_registry_singleton = PluginRegistry()
|
||||
return _registry_singleton
|
||||
|
||||
|
||||
# ── Validation of uploads (ZIP / directory) ────────────────────────────────
|
||||
|
||||
|
||||
def _plugin_manifest_from_member(zf: zipfile.ZipFile) -> dict[str, Any]:
|
||||
try:
|
||||
raw = zf.read(MANIFEST_FILENAME).decode("utf-8")
|
||||
except KeyError:
|
||||
raise ValueError("Plugin zip is missing plugin.json")
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
raise ValueError("Invalid JSON in plugin.json")
|
||||
m = PluginManifest.from_dict(data)
|
||||
return m.to_dict()
|
||||
|
||||
|
||||
def _validate_plugin_zip(zip_path) -> dict[str, Any]:
|
||||
"""Validate an uploaded plugin zip and return its manifest dict."""
|
||||
with zipfile.ZipFile(zip_path, "r") as zf:
|
||||
members = zf.namelist()
|
||||
|
||||
if len(members) > MAX_PLUGIN_FILES:
|
||||
raise ValueError(f"Plugin contains too many files (max {MAX_PLUGIN_FILES})")
|
||||
|
||||
for member in members:
|
||||
if member.endswith("/"):
|
||||
continue
|
||||
norm = member.replace("\\", "/")
|
||||
clean = norm.lstrip("/")
|
||||
if ".." in clean.split("/"):
|
||||
raise ValueError("Path traversal detected in plugin zip")
|
||||
# Reject absolute paths and any ../../ escapes
|
||||
if norm.startswith("/") or "/../" in f"/{norm}" or norm.endswith("/.."):
|
||||
raise ValueError("Path traversal detected in plugin zip")
|
||||
|
||||
manifest = _plugin_manifest_from_member(zf)
|
||||
|
||||
# Entry point must exist
|
||||
main = manifest["main"]
|
||||
if main not in members and f"{main}/" not in members:
|
||||
# accept with any leading ./ or subdir normalization
|
||||
found = any(m.rstrip("/") == main or m.rstrip("/") == f"./{main}" for m in members)
|
||||
if not found:
|
||||
raise ValueError(f"Missing entry point: {main}")
|
||||
return manifest
|
||||
|
||||
|
||||
def _validate_plugin_directory(plugin_dir) -> dict[str, Any]:
|
||||
"""Validate an installed plugin directory and return its manifest dict."""
|
||||
d = Path(plugin_dir)
|
||||
manifest_file = d / MANIFEST_FILENAME
|
||||
if not manifest_file.is_file():
|
||||
raise ValueError(f"Missing {MANIFEST_FILENAME}")
|
||||
try:
|
||||
data = json.loads(manifest_file.read_text(encoding="utf-8"))
|
||||
except json.JSONDecodeError:
|
||||
raise ValueError(f"Invalid JSON in {MANIFEST_FILENAME}")
|
||||
m = PluginManifest.from_dict(data)
|
||||
if not (d / m.main).is_file():
|
||||
raise ValueError(f"Missing entry point: {m.main}")
|
||||
return m.to_dict()
|
||||
|
||||
|
||||
# ── API Endpoints ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@router.get("", response_model=list[dict])
|
||||
async def api_list_plugins(vault: str, current_user=Depends(require_auth)):
|
||||
"""List installed plugins for a vault."""
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
return PluginManager(plugins_dir).list_plugins()
|
||||
|
||||
|
||||
|
||||
@router.get("/template")
|
||||
async def api_plugin_template():
|
||||
"""Return a starter plugin template (manifest + sample code)."""
|
||||
template_manifest = {
|
||||
"name": "my-plugin",
|
||||
"version": "1.0.0",
|
||||
"description": "Describe what your plugin does",
|
||||
"author": "your-name",
|
||||
"main": "index.js",
|
||||
"hooks": {"onFileRender": "render"},
|
||||
"permissions": ["read_files", "ui_notify"],
|
||||
"license": "MIT",
|
||||
}
|
||||
template_code = (
|
||||
"// ObsiGate plugin template\n"
|
||||
"export function render(ctx) {\n"
|
||||
" // ctx: { path, content, extension, vault }\n"
|
||||
" if (ctx.content && ctx.path.endsWith('.md')) {\n"
|
||||
" ctx.content = `> Plugin: ${self.name}\\n\\n` + ctx.content;\n"
|
||||
" }\n"
|
||||
" return ctx;\n"
|
||||
"}\n"
|
||||
)
|
||||
return {
|
||||
"manifest": template_manifest,
|
||||
"code": template_code,
|
||||
}
|
||||
|
||||
@router.get("/{plugin_name}", response_model=dict)
|
||||
async def api_get_plugin(vault: str, plugin_name: str, current_user=Depends(require_auth)):
|
||||
"""Get a single plugin's metadata."""
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
plugin = PluginManager(plugins_dir).get_plugin(plugin_name)
|
||||
if not plugin:
|
||||
raise HTTPException(status_code=404, detail=f"Plugin '{plugin_name}' not found")
|
||||
return plugin
|
||||
|
||||
|
||||
@router.get("/{plugin_name}/hooks", response_model=dict)
|
||||
async def api_plugin_hooks(vault: str, plugin_name: str, current_user=Depends(require_auth)):
|
||||
"""Get the hooks a plugin registers."""
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
try:
|
||||
return PluginManager(plugins_dir).get_hooks(plugin_name)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
|
||||
|
||||
@router.get("/{plugin_name}/code/{file_name:path}", response_model=dict)
|
||||
async def api_plugin_code(
|
||||
vault: str, plugin_name: str, file_name: str, current_user=Depends(require_auth)
|
||||
):
|
||||
"""Get a plugin source file for sandboxed execution."""
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
try:
|
||||
code = PluginManager(plugins_dir).get_plugin_code(plugin_name, file_name)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
return {"name": plugin_name, "file": file_name, "code": code}
|
||||
|
||||
|
||||
@router.post("/install")
|
||||
async def api_install_plugin(
|
||||
vault: str,
|
||||
file: UploadFile = File(...),
|
||||
current_user=Depends(require_admin),
|
||||
):
|
||||
"""Install a plugin from an uploaded zip (admin only)."""
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
|
||||
content = await file.read()
|
||||
if len(content) > 5_000_000: # 5 MB upload cap
|
||||
raise HTTPException(status_code=413, detail="Plugin zip too large")
|
||||
|
||||
import io
|
||||
import tempfile
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
zip_path = Path(tmp_dir) / "plugin.zip"
|
||||
try:
|
||||
zip_path.write_bytes(content)
|
||||
manifest = _validate_plugin_zip(zip_path)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid plugin: {exc}")
|
||||
|
||||
# Extract to a temp dir then validate the entry point exists
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
manager = PluginManager(plugins_dir)
|
||||
|
||||
name = manifest["name"]
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
try:
|
||||
with zipfile.ZipFile(io.BytesIO(content)) as zf:
|
||||
zf.extractall(td)
|
||||
except (zipfile.BadZipFile, RuntimeError):
|
||||
raise HTTPException(status_code=400, detail="Invalid plugin zip")
|
||||
extracted = _validate_plugin_directory(Path(td) / name if (Path(td) / name).is_dir() else Path(td))
|
||||
|
||||
code_file = manifest["main"]
|
||||
with zipfile.ZipFile(io.BytesIO(content)) as zf:
|
||||
code = zf.read(code_file).decode("utf-8")
|
||||
|
||||
try:
|
||||
result = manager.install(extracted, code)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc))
|
||||
return result
|
||||
|
||||
|
||||
@router.delete("/{plugin_name}")
|
||||
async def api_uninstall_plugin(vault: str, plugin_name: str, current_user=Depends(require_admin)):
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
try:
|
||||
PluginManager(plugins_dir).uninstall(plugin_name)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
return JSONResponse({"ok": True})
|
||||
|
||||
|
||||
@router.post("/{plugin_name}/enable")
|
||||
async def api_enable_plugin(vault: str, plugin_name: str, current_user=Depends(require_admin)):
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
try:
|
||||
PluginManager(plugins_dir).enable(plugin_name)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
return JSONResponse({"ok": True})
|
||||
|
||||
|
||||
@router.post("/{plugin_name}/disable")
|
||||
async def api_disable_plugin(vault: str, plugin_name: str, current_user=Depends(require_admin)):
|
||||
if not check_vault_access(vault, current_user):
|
||||
raise HTTPException(status_code=403, detail=f"Accès refusé à la vault '{vault}'")
|
||||
plugins_dir = _vault_plugins_dir(vault)
|
||||
try:
|
||||
PluginManager(plugins_dir).disable(plugin_name)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
return JSONResponse({"ok": True})
|
||||
|
||||
# ── Plugin Event Bus ────────────────────────────────────────────────────────
|
||||
# Lightweight pub/sub for backend events that plugins can subscribe to.
|
||||
# Used to dispatch onFileCreate, onFileDelete, onVaultMount to enabled plugins.
|
||||
|
||||
|
||||
class _PluginEventBus:
|
||||
"""In-memory event dispatcher for plugin lifecycle events."""
|
||||
_listeners: ClassVar[dict[str, list]] = {}
|
||||
|
||||
@classmethod
|
||||
def subscribe(cls, event_type: str, callback) -> None:
|
||||
cls._listeners.setdefault(event_type, []).append(callback)
|
||||
|
||||
@classmethod
|
||||
async def emit(cls, event_type: str, data: dict) -> None:
|
||||
for cb in cls._listeners.get(event_type, []):
|
||||
try:
|
||||
result = cb(data)
|
||||
if hasattr(result, "__await__"):
|
||||
await result
|
||||
except Exception as e:
|
||||
logger.warning(f"Plugin event '{event_type}' handler error: {e}")
|
||||
|
||||
|
||||
plugin_events = _PluginEventBus
|
||||
|
||||
|
||||
def emit_file_created(vault: str, path: str, title: str = "") -> None:
|
||||
"""Called when a file is created via the API."""
|
||||
import asyncio
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.create_task(plugin_events.emit("onFileCreate", {
|
||||
"vault": vault, "path": path, "title": title,
|
||||
"timestamp": _time.time(),
|
||||
}))
|
||||
except RuntimeError:
|
||||
pass # No event loop running
|
||||
|
||||
|
||||
def emit_file_deleted(vault: str, path: str) -> None:
|
||||
"""Called when a file is deleted via the API."""
|
||||
import asyncio
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.create_task(plugin_events.emit("onFileDelete", {
|
||||
"vault": vault, "path": path,
|
||||
"timestamp": _time.time(),
|
||||
}))
|
||||
except RuntimeError:
|
||||
pass
|
||||
|
||||
|
||||
def emit_vault_mounted(vault: str, path: str) -> None:
|
||||
"""Called when a vault is loaded into the index at startup."""
|
||||
import asyncio
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.create_task(plugin_events.emit("onVaultMount", {
|
||||
"vault": vault, "path": path,
|
||||
"timestamp": _time.time(),
|
||||
}))
|
||||
except RuntimeError:
|
||||
pass
|
||||
@@ -0,0 +1,174 @@
|
||||
"""Provider-declared model capabilities (live) with a short-lived cache.
|
||||
|
||||
The curated table in :mod:`backend.model_capabilities` has to be edited by hand
|
||||
every time a provider ships or renames a model, and it ages badly: Mistral alone
|
||||
declares ``vision`` on 28 of its models while the curated table knew none of
|
||||
them (BUG-044). Providers that *do* publish per-model capabilities in their
|
||||
public models endpoint are therefore asked first; the curated table is only
|
||||
used to fill the flags the provider stays silent about (e.g. Mistral never
|
||||
declares ``embedding``, only the absence of ``completion_chat``).
|
||||
|
||||
Supported declarations, detected by *payload shape* so a provider that starts
|
||||
exposing them is picked up without a code change:
|
||||
|
||||
* ``capabilities`` dict (Mistral): ``completion_chat`` → ``chat``, ``vision``,
|
||||
``audio_transcription`` (+ ``audio_transcription_realtime``),
|
||||
``audio_speech``.
|
||||
* ``architecture`` dict (OpenRouter): ``input_modalities`` /
|
||||
``output_modalities`` → ``vision`` (image input), ``images`` (image output),
|
||||
``audio_transcription`` (audio input), ``audio_speech`` (audio output),
|
||||
``video`` (video output), ``chat`` (text output).
|
||||
|
||||
The cache is in-process and shared by every request. Its TTL only bounds how
|
||||
long a *stale* declaration can survive: a fresh provider call (the model list
|
||||
endpoint) overwrites the provider entry immediately.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.ai.capabilities")
|
||||
|
||||
#: How long a provider-declared snapshot stays usable, in seconds.
|
||||
#: ``0`` disables expiry (the snapshot lives until the next provider call).
|
||||
TTL_SECONDS = float(os.getenv("AI_CAPABILITIES_TTL_SECONDS", "1800") or 0)
|
||||
|
||||
#: Guard against a provider returning a runaway model list.
|
||||
MAX_MODELS_PER_PROVIDER = 2000
|
||||
|
||||
# provider → (timestamp, model count, {normalized model id: declared flags})
|
||||
_CACHE: dict[str, tuple[float, int, dict[str, dict[str, bool]]]] = {}
|
||||
|
||||
|
||||
def _norm(value: str) -> str:
|
||||
"""Lower-case and strip the ``models/`` prefix Gemini uses."""
|
||||
return (value or "").strip().lower().removeprefix("models/")
|
||||
|
||||
|
||||
def _from_capability_flags(flags: dict[str, Any]) -> dict[str, bool]:
|
||||
"""Map a Mistral-style ``capabilities`` dict onto ObsiGate flags."""
|
||||
declared: dict[str, bool] = {}
|
||||
if isinstance(flags.get("completion_chat"), bool):
|
||||
declared["chat"] = flags["completion_chat"]
|
||||
if isinstance(flags.get("vision"), bool):
|
||||
declared["vision"] = flags["vision"]
|
||||
transcription = flags.get("audio_transcription")
|
||||
realtime = flags.get("audio_transcription_realtime")
|
||||
if isinstance(transcription, bool) or isinstance(realtime, bool):
|
||||
declared["audio_transcription"] = bool(transcription or realtime)
|
||||
if isinstance(flags.get("audio_speech"), bool):
|
||||
declared["audio_speech"] = flags["audio_speech"]
|
||||
return declared
|
||||
|
||||
|
||||
def _from_architecture(architecture: dict[str, Any]) -> dict[str, bool]:
|
||||
"""Map an OpenRouter-style ``architecture`` dict onto ObsiGate flags."""
|
||||
inputs = architecture.get("input_modalities")
|
||||
outputs = architecture.get("output_modalities")
|
||||
if not isinstance(inputs, list) and not isinstance(outputs, list):
|
||||
return {}
|
||||
in_modalities = [str(m).lower() for m in inputs] if isinstance(inputs, list) else []
|
||||
out_modalities = [str(m).lower() for m in outputs] if isinstance(outputs, list) else []
|
||||
return {
|
||||
"chat": "text" in out_modalities,
|
||||
"vision": "image" in in_modalities,
|
||||
"images": "image" in out_modalities,
|
||||
"audio_transcription": "audio" in in_modalities,
|
||||
"audio_speech": "audio" in out_modalities,
|
||||
"video": "video" in out_modalities,
|
||||
}
|
||||
|
||||
|
||||
def parse_declared_capabilities(entry: Any) -> dict[str, bool] | None:
|
||||
"""Extract the capability flags a single provider model entry declares.
|
||||
|
||||
Args:
|
||||
entry: One item of a provider models payload (``/v1/models``).
|
||||
|
||||
Returns:
|
||||
A partial ``{flag: bool}`` mapping (only the flags the provider
|
||||
actually declares), or ``None`` when the entry declares nothing.
|
||||
"""
|
||||
if not isinstance(entry, dict):
|
||||
return None
|
||||
flags = entry.get("capabilities")
|
||||
declared = _from_capability_flags(flags) if isinstance(flags, dict) else {}
|
||||
if not declared:
|
||||
architecture = entry.get("architecture")
|
||||
declared = _from_architecture(architecture) if isinstance(architecture, dict) else {}
|
||||
return declared or None
|
||||
|
||||
|
||||
def remember_declared_capabilities(provider: str, payload: Any) -> int:
|
||||
"""Cache the capabilities declared by a provider models payload.
|
||||
|
||||
Args:
|
||||
provider: Provider identifier (e.g. ``"mistral"``).
|
||||
payload: Raw JSON body of the provider models endpoint, or the model
|
||||
list itself.
|
||||
|
||||
Returns:
|
||||
The number of models with declared capabilities that were cached.
|
||||
"""
|
||||
provider = (provider or "").strip().lower()
|
||||
entries: Any = payload.get("data") if isinstance(payload, dict) else payload
|
||||
if not isinstance(entries, list):
|
||||
return 0
|
||||
|
||||
parsed: dict[str, dict[str, bool]] = {}
|
||||
for entry in entries[:MAX_MODELS_PER_PROVIDER]:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
model_id = entry.get("id") or entry.get("name") or ""
|
||||
if not isinstance(model_id, str) or not model_id.strip():
|
||||
continue
|
||||
declared = parse_declared_capabilities(entry)
|
||||
if declared:
|
||||
parsed[_norm(model_id)] = declared
|
||||
|
||||
if not parsed:
|
||||
return 0
|
||||
_CACHE[provider] = (time.time(), len(parsed), parsed)
|
||||
logger.info(f"Capabilities declared by {provider}: {len(parsed)} models cached")
|
||||
return len(parsed)
|
||||
|
||||
|
||||
def get_declared_capabilities(provider: str, model: str) -> dict[str, bool] | None:
|
||||
"""Return the cached declared capabilities for one provider/model pair.
|
||||
|
||||
Returns ``None`` when nothing was declared for that pair (cache cold,
|
||||
expired, or the provider is silent about this model).
|
||||
"""
|
||||
snapshot = _CACHE.get((provider or "").strip().lower())
|
||||
if not snapshot:
|
||||
return None
|
||||
timestamp, _count, table = snapshot
|
||||
if TTL_SECONDS and (time.time() - timestamp) > TTL_SECONDS:
|
||||
return None
|
||||
declared = table.get(_norm(model))
|
||||
return dict(declared) if declared else None
|
||||
|
||||
|
||||
def clear_declared_capabilities(provider: str | None = None) -> None:
|
||||
"""Drop the cached snapshot for one provider, or all of them (tests/ops)."""
|
||||
if provider is None:
|
||||
_CACHE.clear()
|
||||
return
|
||||
_CACHE.pop(provider.strip().lower(), None)
|
||||
|
||||
|
||||
def cache_info() -> dict[str, dict[str, Any]]:
|
||||
"""Diagnostics: per-provider cache age and model count."""
|
||||
now = time.time()
|
||||
return {
|
||||
provider: {
|
||||
"models": count,
|
||||
"age_seconds": round(now - timestamp, 1),
|
||||
"expired": bool(TTL_SECONDS and (now - timestamp) > TTL_SECONDS),
|
||||
}
|
||||
for provider, (timestamp, count, _table) in _CACHE.items()
|
||||
}
|
||||
+285
@@ -0,0 +1,285 @@
|
||||
# backend/push.py
|
||||
# Push Notifications — Web Push API with VAPID authentication
|
||||
# ROADMAP #67
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.auth.middleware import require_auth
|
||||
|
||||
logger = logging.getLogger("obsigate.push")
|
||||
|
||||
router = APIRouter(prefix="/api/push", tags=["push"])
|
||||
|
||||
# VAPID keys storage
|
||||
VAPID_KEYS_FILE = Path(os.environ.get("OBSIGATE_DATA_DIR", "data")) / "vapid_keys.json"
|
||||
PUSH_SUBSCRIPTIONS_FILE = Path(os.environ.get("OBSIGATE_DATA_DIR", "data")) / "push_subscriptions.json"
|
||||
|
||||
# In-memory cache
|
||||
_vapid_keys: dict[str, str] = {}
|
||||
_push_subscriptions: list[dict[str, Any]] = []
|
||||
|
||||
|
||||
def load_vapid_keys() -> dict[str, str]:
|
||||
"""Load VAPID keys from file or generate new ones."""
|
||||
global _vapid_keys
|
||||
if _vapid_keys:
|
||||
return _vapid_keys
|
||||
|
||||
try:
|
||||
if VAPID_KEYS_FILE.exists():
|
||||
with open(VAPID_KEYS_FILE, "r") as f:
|
||||
_vapid_keys = json.load(f)
|
||||
else:
|
||||
# Generate new VAPID keys
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
|
||||
private_key = ec.generate_private_key(ec.SECP256R1())
|
||||
public_key = private_key.public_key()
|
||||
|
||||
private_pem = private_key.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.PKCS8,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
).decode('utf-8')
|
||||
|
||||
public_pem = public_key.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
|
||||
# Convert to base64url for Web Push
|
||||
public_numbers = public_key.public_numbers()
|
||||
|
||||
def int_to_base64url(n: int) -> str:
|
||||
byte_len = (n.bit_length() + 7) // 8
|
||||
return base64.urlsafe_b64encode(n.to_bytes(byte_len, 'big')).decode('utf-8').rstrip('=')
|
||||
|
||||
_vapid_keys = {
|
||||
"private_key": private_pem,
|
||||
"public_key": public_pem,
|
||||
"public_key_base64url": int_to_base64url(public_numbers.x) + "." + int_to_base64url(public_numbers.y)
|
||||
}
|
||||
|
||||
VAPID_KEYS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(VAPID_KEYS_FILE, "w") as f:
|
||||
json.dump(_vapid_keys, f)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load/generate VAPID keys: {e}")
|
||||
# Fallback: return empty (will use demo keys if needed)
|
||||
_vapid_keys = {}
|
||||
|
||||
return _vapid_keys
|
||||
|
||||
|
||||
def get_vapid_public_key() -> str:
|
||||
"""Get VAPID public key in base64url format for client."""
|
||||
keys = load_vapid_keys()
|
||||
return keys.get("public_key_base64url", "")
|
||||
|
||||
|
||||
def load_push_subscriptions() -> list[dict[str, Any]]:
|
||||
"""Load push subscriptions from file."""
|
||||
global _push_subscriptions
|
||||
if _push_subscriptions:
|
||||
return _push_subscriptions
|
||||
|
||||
try:
|
||||
if PUSH_SUBSCRIPTIONS_FILE.exists():
|
||||
with open(PUSH_SUBSCRIPTIONS_FILE, "r") as f:
|
||||
_push_subscriptions = json.load(f)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load push subscriptions: {e}")
|
||||
_push_subscriptions = []
|
||||
return _push_subscriptions
|
||||
|
||||
|
||||
def save_push_subscriptions() -> None:
|
||||
"""Save push subscriptions to file."""
|
||||
try:
|
||||
PUSH_SUBSCRIPTIONS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(PUSH_SUBSCRIPTIONS_FILE, "w") as f:
|
||||
json.dump(_push_subscriptions, f, indent=2)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save push subscriptions: {e}")
|
||||
|
||||
|
||||
# ── Models ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class PushSubscription(BaseModel):
|
||||
"""Push subscription from client (matches Push API)."""
|
||||
endpoint: str
|
||||
keys: dict[str, str] # { p256dh, auth }
|
||||
|
||||
|
||||
class SubscribeRequest(BaseModel):
|
||||
"""Request to subscribe to push notifications."""
|
||||
subscription: PushSubscription
|
||||
vault: str = Field(description="Vault name this subscription is for")
|
||||
|
||||
|
||||
class SubscribeResponse(BaseModel):
|
||||
"""Response to subscription request."""
|
||||
success: bool
|
||||
subscription_id: str | None = None
|
||||
|
||||
|
||||
class VapidPublicKeyResponse(BaseModel):
|
||||
"""VAPID public key for client."""
|
||||
public_key: str
|
||||
|
||||
|
||||
class PushPayload(BaseModel):
|
||||
"""Payload for sending a push notification."""
|
||||
vault: str
|
||||
title: str
|
||||
body: str
|
||||
data: dict[str, Any] = Field(default_factory=dict)
|
||||
tag: str = "obsigate-notification"
|
||||
|
||||
|
||||
# ── Endpoints ───────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/vapid-public-key", response_model=VapidPublicKeyResponse)
|
||||
async def get_vapid_public_key_endpoint():
|
||||
"""Get VAPID public key for client subscription."""
|
||||
public_key = get_vapid_public_key()
|
||||
if not public_key:
|
||||
# Return a demo key if generation failed (for testing)
|
||||
return {"public_key": "demo-key-for-testing"}
|
||||
return {"public_key": public_key}
|
||||
|
||||
|
||||
@router.post("/subscribe", response_model=SubscribeResponse)
|
||||
async def subscribe_push(
|
||||
request: SubscribeRequest,
|
||||
current_user=Depends(require_auth)
|
||||
):
|
||||
"""Subscribe to push notifications for a vault."""
|
||||
username = current_user.get("username", "unknown")
|
||||
|
||||
# Check if user has access to this vault
|
||||
user_vaults = current_user.get("_token_vaults") or current_user.get("vaults", [])
|
||||
if "*" not in user_vaults and request.vault not in user_vaults:
|
||||
raise HTTPException(status_code=403, detail="No access to this vault")
|
||||
|
||||
# Check if subscription already exists
|
||||
for sub in _push_subscriptions:
|
||||
if sub["endpoint"] == request.subscription.endpoint and sub["username"] == username:
|
||||
return SubscribeResponse(success=True, subscription_id=sub.get("id"))
|
||||
|
||||
# Add new subscription
|
||||
sub_id = base64.urlsafe_b64encode(os.urandom(16)).decode('utf-8').rstrip('=')
|
||||
subscription = {
|
||||
"id": sub_id,
|
||||
"endpoint": request.subscription.endpoint,
|
||||
"keys": request.subscription.keys,
|
||||
"vault": request.vault,
|
||||
"username": username,
|
||||
"created_at": datetime.now(timezone.utc).isoformat()
|
||||
}
|
||||
|
||||
_push_subscriptions.append(subscription)
|
||||
save_push_subscriptions()
|
||||
|
||||
logger.info(f"Push subscription added for user={username}, vault={request.vault}")
|
||||
return SubscribeResponse(success=True, subscription_id=sub_id)
|
||||
|
||||
|
||||
@router.delete("/subscribe")
|
||||
async def unsubscribe_push(
|
||||
endpoint: str,
|
||||
current_user=Depends(require_auth)
|
||||
):
|
||||
"""Unsubscribe from push notifications."""
|
||||
username = current_user.get("username", "unknown")
|
||||
|
||||
global _push_subscriptions
|
||||
original_len = len(_push_subscriptions)
|
||||
_push_subscriptions = [
|
||||
s for s in _push_subscriptions
|
||||
if not (s["endpoint"] == endpoint and s["username"] == username)
|
||||
]
|
||||
|
||||
if len(_push_subscriptions) < original_len:
|
||||
save_push_subscriptions()
|
||||
return {"success": True, "message": "Unsubscribed"}
|
||||
|
||||
return {"success": False, "message": "Subscription not found"}
|
||||
|
||||
|
||||
@router.get("/subscriptions")
|
||||
async def list_subscriptions(current_user=Depends(require_auth)):
|
||||
"""List current user's push subscriptions."""
|
||||
username = current_user.get("username", "unknown")
|
||||
user_subs = [
|
||||
{
|
||||
"id": s["id"],
|
||||
"vault": s["vault"],
|
||||
"created_at": s["created_at"],
|
||||
"endpoint": s["endpoint"][:50] + "..." # Truncate for privacy
|
||||
}
|
||||
for s in _push_subscriptions if s["username"] == username
|
||||
]
|
||||
return {"subscriptions": user_subs}
|
||||
|
||||
|
||||
# ── Internal: Send push notification ────────────────────────────────────────
|
||||
|
||||
async def send_push_notification(vault: str, title: str, body: str, data: dict | None = None, tag: str = "obsigate-notification") -> int:
|
||||
"""Send push notification to all subscribers of a vault. Returns count of sent notifications."""
|
||||
subscriptions = [s for s in _push_subscriptions if s["vault"] == vault]
|
||||
|
||||
if not subscriptions:
|
||||
return 0
|
||||
|
||||
payload = {
|
||||
"title": title,
|
||||
"body": body,
|
||||
"data": data or {},
|
||||
"tag": tag
|
||||
}
|
||||
|
||||
# Import here to avoid circular dependency
|
||||
from pywebpush import WebPushException, webpush
|
||||
|
||||
keys = load_vapid_keys()
|
||||
vapid_private_key = keys.get("private_key")
|
||||
vapid_claims = {
|
||||
"sub": "mailto:[email protected]"
|
||||
}
|
||||
|
||||
sent = 0
|
||||
for sub in subscriptions:
|
||||
try:
|
||||
webpush(
|
||||
subscription_info={
|
||||
"endpoint": sub["endpoint"],
|
||||
"keys": sub["keys"]
|
||||
},
|
||||
data=json.dumps(payload),
|
||||
vapid_private_key=vapid_private_key,
|
||||
vapid_claims=vapid_claims
|
||||
)
|
||||
sent += 1
|
||||
except WebPushException as e:
|
||||
logger.warning(f"Push failed for subscription {sub['id']}: {e}")
|
||||
# If subscription expired/invalid, remove it
|
||||
if e.response and e.response.status_code in (404, 410):
|
||||
_push_subscriptions.remove(sub)
|
||||
except Exception as e:
|
||||
logger.error(f"Push error for {sub['id']}: {e}")
|
||||
|
||||
if sent < len(subscriptions):
|
||||
save_push_subscriptions()
|
||||
|
||||
return sent
|
||||
+67
-19
@@ -1,51 +1,66 @@
|
||||
"""
|
||||
IP-based rate limiter for authentication endpoints.
|
||||
In-memory rate limiter for authentication endpoints.
|
||||
|
||||
Tracks failed login attempts per IP address with automatic
|
||||
cleanup of expired entries. Complements the per-account lockout
|
||||
in user_store.py.
|
||||
Tracks failed attempts per IP **and** per account with automatic cleanup of
|
||||
expired entries. The per-IP budget stops a single source; the per-account
|
||||
budget (BUG-031) still throttles an attacker who rotates IPs. It complements
|
||||
the per-account lockout in ``user_store.py``.
|
||||
|
||||
.. note::
|
||||
The counters live in process memory only. They are **not** shared between
|
||||
multiple workers/containers and are lost on restart. For a multi-node
|
||||
deployment, front this service with a shared store (Redis) or a single
|
||||
worker. This limitation is intentional and documented (BUG-031).
|
||||
|
||||
Configuration via environment variables:
|
||||
OBSIGATE_LOGIN_MAX_ATTEMPTS Max failures per IP (default: 10)
|
||||
OBSIGATE_LOGIN_WINDOW_SECONDS Lockout window in seconds (default: 900 = 15min)
|
||||
OBSIGATE_LOGIN_MAX_ATTEMPTS Max failures per IP (default: 10)
|
||||
OBSIGATE_ACCOUNT_MAX_ATTEMPTS Max failures per account (default: 10)
|
||||
OBSIGATE_LOGIN_WINDOW_SECONDS Lockout window in seconds (default: 900)
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
from typing import Dict, Tuple
|
||||
|
||||
logger = logging.getLogger("obsigate.ratelimit")
|
||||
|
||||
# --- Configuration ---
|
||||
MAX_ATTEMPTS = int(os.environ.get("OBSIGATE_LOGIN_MAX_ATTEMPTS", "10"))
|
||||
ACCOUNT_MAX_ATTEMPTS = int(os.environ.get("OBSIGATE_ACCOUNT_MAX_ATTEMPTS", "10"))
|
||||
WINDOW_SECONDS = int(os.environ.get("OBSIGATE_LOGIN_WINDOW_SECONDS", "900")) # 15 min
|
||||
|
||||
# --- In-memory store: {ip: [(timestamp, success_bool), ...]} ---
|
||||
_ip_attempts: Dict[str, list] = defaultdict(list)
|
||||
# --- In-memory stores: {key: [(timestamp, success_bool), ...]} ---
|
||||
_ip_attempts: dict[str, list] = defaultdict(list)
|
||||
_account_attempts: dict[str, list] = defaultdict(list)
|
||||
_last_cleanup = time.time()
|
||||
CLEANUP_INTERVAL = 60 # seconds
|
||||
|
||||
|
||||
def _prune(store: dict[str, list], cutoff: float) -> None:
|
||||
"""Drop expired entries from one store in place."""
|
||||
expired = []
|
||||
for key, attempts in store.items():
|
||||
store[key] = [a for a in attempts if a[0] > cutoff]
|
||||
if not store[key]:
|
||||
expired.append(key)
|
||||
for key in expired:
|
||||
del store[key]
|
||||
|
||||
|
||||
def _cleanup_expired():
|
||||
"""Remove entries older than the window."""
|
||||
"""Remove entries older than the window from both stores."""
|
||||
global _last_cleanup
|
||||
now = time.time()
|
||||
if now - _last_cleanup < CLEANUP_INTERVAL:
|
||||
return
|
||||
_last_cleanup = now
|
||||
cutoff = now - WINDOW_SECONDS
|
||||
expired_ips = []
|
||||
for ip, attempts in _ip_attempts.items():
|
||||
_ip_attempts[ip] = [a for a in attempts if a[0] > cutoff]
|
||||
if not _ip_attempts[ip]:
|
||||
expired_ips.append(ip)
|
||||
for ip in expired_ips:
|
||||
del _ip_attempts[ip]
|
||||
_prune(_ip_attempts, cutoff)
|
||||
_prune(_account_attempts, cutoff)
|
||||
|
||||
|
||||
def record_failure(ip: str) -> Tuple[int, int]:
|
||||
def record_failure(ip: str) -> tuple[int, int]:
|
||||
"""Record a failed login attempt from an IP.
|
||||
|
||||
Returns:
|
||||
@@ -73,6 +88,37 @@ def is_rate_limited(ip: str) -> bool:
|
||||
return failures >= MAX_ATTEMPTS
|
||||
|
||||
|
||||
def record_account_failure(account: str) -> tuple[int, int]:
|
||||
"""Record a failed attempt for an account, regardless of source IP.
|
||||
|
||||
Returns:
|
||||
(current_failure_count, remaining_attempts)
|
||||
"""
|
||||
_cleanup_expired()
|
||||
key = account.lower()
|
||||
_account_attempts[key].append((time.time(), False))
|
||||
failures = sum(1 for _, success in _account_attempts[key] if not success)
|
||||
remaining = max(0, ACCOUNT_MAX_ATTEMPTS - failures)
|
||||
if failures >= ACCOUNT_MAX_ATTEMPTS:
|
||||
logger.warning(f"Account {account} rate-limited after {failures} failed attempts")
|
||||
return failures, remaining
|
||||
|
||||
|
||||
def record_account_success(account: str):
|
||||
"""Clear the per-account rate limit state after a successful login."""
|
||||
_cleanup_expired()
|
||||
_account_attempts[account.lower()] = [(time.time(), True)]
|
||||
|
||||
|
||||
def is_account_rate_limited(account: str) -> bool:
|
||||
"""Check if an account has exceeded the per-account rate limit."""
|
||||
_cleanup_expired()
|
||||
failures = sum(
|
||||
1 for _, success in _account_attempts.get(account.lower(), []) if not success
|
||||
)
|
||||
return failures >= ACCOUNT_MAX_ATTEMPTS
|
||||
|
||||
|
||||
def get_status(ip: str | None = None) -> dict:
|
||||
"""Get rate limit status for an IP (for diagnostics)."""
|
||||
_cleanup_expired()
|
||||
@@ -88,7 +134,9 @@ def get_status(ip: str | None = None) -> dict:
|
||||
}
|
||||
return {
|
||||
"tracked_ips": len(_ip_attempts),
|
||||
"tracked_accounts": len(_account_attempts),
|
||||
"max_attempts": MAX_ATTEMPTS,
|
||||
"account_max_attempts": ACCOUNT_MAX_ATTEMPTS,
|
||||
"window_seconds": WINDOW_SECONDS,
|
||||
"limited_ips": sum(
|
||||
1 for ip_addr in _ip_attempts
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# ObsiGate — Optional dependencies for semantic search (#70)
|
||||
#
|
||||
# These are NOT required: the semantic search engine degrades gracefully to a
|
||||
# dependency-free hashing embedder and a pure-Python cosine store when they are
|
||||
# absent. Install this file to enable the full local model + fast vector index:
|
||||
#
|
||||
# pip install -r backend/requirements-semantic.txt
|
||||
#
|
||||
# NOTE: sentence-transformers pulls in PyTorch (large download). If you only
|
||||
# want the vector acceleration, install numpy + faiss-cpu and configure an
|
||||
# external embedding endpoint instead (OBSIGATE_EMBEDDING_*).
|
||||
|
||||
# Local embedding model (all-MiniLM-L6-v2, ~80 MB, CPU)
|
||||
sentence-transformers>=2.2.0
|
||||
|
||||
# Vector storage / similarity search
|
||||
numpy>=1.24.0
|
||||
faiss-cpu>=1.7.4
|
||||
@@ -0,0 +1,5 @@
|
||||
# Additional dev/test-only dependencies for the test suite.
|
||||
# These are NOT required for production runtime.
|
||||
# Install with: pip install -r requirements-test.txt
|
||||
|
||||
reportlab>=4.0 # PDF fixture generation for test_pdf.py
|
||||
@@ -1,5 +1,6 @@
|
||||
fastapi==0.110.3
|
||||
uvicorn==0.30.0
|
||||
websockets>=12.0
|
||||
python-frontmatter==1.1.0
|
||||
mistune==3.0.2
|
||||
python-multipart==0.0.9
|
||||
@@ -12,3 +13,10 @@ sortedcontainers>=2.4.0
|
||||
snowballstemmer>=2.2.0
|
||||
weasyprint>=60.0
|
||||
httpx>=0.27.0
|
||||
pypdf>=4.0
|
||||
pyotp>=2.10.0
|
||||
webauthn==2.6.0
|
||||
psutil>=5.9
|
||||
pywebpush>=2.3.0
|
||||
mcp==1.9.4
|
||||
sse-starlette==2.1.3
|
||||
|
||||
@@ -5,11 +5,11 @@ Stored in data/saved_searches.json per user.
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
import shutil
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.saved_searches")
|
||||
|
||||
@@ -21,7 +21,7 @@ def _get_file(username: str) -> Path:
|
||||
return DATA_DIR / f"{username}_saved_searches.json"
|
||||
|
||||
|
||||
def _read(file: Path) -> List[Dict[str, Any]]:
|
||||
def _read(file: Path) -> list[dict[str, Any]]:
|
||||
if not file.exists():
|
||||
return []
|
||||
try:
|
||||
@@ -30,13 +30,13 @@ def _read(file: Path) -> List[Dict[str, Any]]:
|
||||
return []
|
||||
|
||||
|
||||
def _write(file: Path, data: List[Dict[str, Any]]):
|
||||
def _write(file: Path, data: list[dict[str, Any]]):
|
||||
tmp = file.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
|
||||
shutil.move(str(tmp), str(file))
|
||||
|
||||
|
||||
def get_saved(username: str) -> List[Dict[str, Any]]:
|
||||
def get_saved(username: str) -> list[dict[str, Any]]:
|
||||
return _read(_get_file(username))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,517 @@
|
||||
"""Additional Pydantic response models for the ObsiGate REST API.
|
||||
|
||||
These models enrich the auto-generated OpenAPI 3.1 schema (#72). They are
|
||||
intentionally permissive (``extra="allow"``) so that adding a new field to an
|
||||
existing endpoint never breaks response validation — the model documents the
|
||||
stable, public shape while still accepting internal additions.
|
||||
|
||||
Models whose endpoints are already typed in :mod:`backend.main` (e.g.
|
||||
``FileContentResponse``) live there; this module only holds the ones used to
|
||||
fill the gaps.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Generic helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class StatusResponse(BaseModel):
|
||||
"""Generic ``{"status": "..."}`` acknowledgement."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
status: str = Field(default="ok", description="Operation status")
|
||||
|
||||
|
||||
class ErrorResponse(BaseModel):
|
||||
"""Standard FastAPI error envelope."""
|
||||
|
||||
detail: str | list[dict[str, Any]] | None = Field(
|
||||
default=None,
|
||||
description="Human-readable error message, or a list of validation errors",
|
||||
examples=["Vault 'Notes' not found"],
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Recent files & bookmarks
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class RecentFileItem(BaseModel):
|
||||
"""A recently opened or recently modified file."""
|
||||
|
||||
path: str = Field(description="Relative path within the vault")
|
||||
title: str = Field(description="File title")
|
||||
vault: str = Field(description="Vault name")
|
||||
mtime: float | int | str | None = Field(default=None, description="Modification timestamp")
|
||||
mtime_human: str | None = Field(default=None, description="Human-readable modification time")
|
||||
mtime_iso: str | None = Field(default=None, description="ISO-8601 modification time")
|
||||
size_bytes: int | None = Field(default=None, description="File size in bytes")
|
||||
tags: list[str] = Field(default_factory=list, description="Up to 5 leading tags")
|
||||
preview: str | None = Field(default=None, description="Short content preview")
|
||||
bookmarked: bool | None = Field(default=None, description="Whether the file is bookmarked")
|
||||
|
||||
|
||||
class RecentResponse(BaseModel):
|
||||
"""Response for ``GET /api/recent``."""
|
||||
|
||||
files: list[RecentFileItem]
|
||||
total: int = Field(description="Total number of files in the selected mode")
|
||||
limit: int = Field(description="Applied limit")
|
||||
mode: str = Field(description="'opened' or 'modified'")
|
||||
|
||||
model_config = ConfigDict(
|
||||
json_schema_extra={
|
||||
"example": {
|
||||
"files": [
|
||||
{
|
||||
"path": "notes/Accueil.md",
|
||||
"title": "Accueil",
|
||||
"vault": "TestVault",
|
||||
"mtime": 1750000000.0,
|
||||
"mtime_human": "il y a 2 h",
|
||||
"size_bytes": 1024,
|
||||
"tags": ["#accueil"],
|
||||
"preview": "# Bienvenue…",
|
||||
"bookmarked": False,
|
||||
}
|
||||
],
|
||||
"total": 1,
|
||||
"limit": 20,
|
||||
"mode": "opened",
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class BookmarkFileItem(BaseModel):
|
||||
"""A bookmarked file."""
|
||||
|
||||
path: str = Field(description="Relative path within the vault")
|
||||
title: str = Field(description="File title")
|
||||
vault: str = Field(description="Vault name")
|
||||
mtime: float | int | str | None = Field(default=None, description="Bookmark timestamp")
|
||||
mtime_human: str | None = Field(default=None, description="Human-readable bookmark time")
|
||||
size_bytes: int | None = Field(default=None, description="File size in bytes")
|
||||
tags: list[str] = Field(default_factory=list, description="Up to 5 leading tags")
|
||||
bookmarked: bool | None = Field(default=True, description="Always true for this endpoint")
|
||||
|
||||
|
||||
class BookmarksResponse(BaseModel):
|
||||
"""Response for ``GET /api/bookmarks``."""
|
||||
|
||||
files: list[BookmarkFileItem]
|
||||
total: int = Field(description="Number of bookmarked files")
|
||||
|
||||
|
||||
class BookmarkToggleResponse(BaseModel):
|
||||
"""Response for ``POST /api/bookmarks/toggle``."""
|
||||
|
||||
bookmarked: bool = Field(description="New bookmark state")
|
||||
|
||||
|
||||
class SavedSearch(BaseModel):
|
||||
"""A persisted search definition."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
id: str = Field(description="Unique identifier (millisecond timestamp)")
|
||||
query: str = Field(description="Search query text")
|
||||
vault: str = Field(default="all", description="Vault filter")
|
||||
case_sensitive: bool = Field(default=False)
|
||||
whole_word: bool = Field(default=False)
|
||||
regex: bool = Field(default=False)
|
||||
include_paths: str = Field(default="")
|
||||
exclude_paths: str = Field(default="")
|
||||
created_at: float = Field(description="Creation timestamp (epoch seconds)")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Backups & diffs
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class BackupsResponse(BaseModel):
|
||||
"""Response for ``GET /api/file/{vault}/backups``."""
|
||||
|
||||
vault: str
|
||||
path: str
|
||||
backups: list[dict[str, Any]] = Field(description="Backups, newest first")
|
||||
|
||||
|
||||
class BacklinksResponse(BaseModel):
|
||||
"""Response for ``GET /api/file/{vault}/backlinks``."""
|
||||
|
||||
vault: str
|
||||
path: str
|
||||
backlinks: list[dict[str, Any]] = Field(description="Files linking to the target")
|
||||
total: int
|
||||
|
||||
|
||||
class BackupsListResponse(BaseModel):
|
||||
"""Response for ``GET /api/backups``."""
|
||||
|
||||
backups: list[dict[str, Any]] = Field(description="All backups across vaults")
|
||||
total: int
|
||||
total_size_bytes: int
|
||||
|
||||
|
||||
class BackupsDeletedResponse(BaseModel):
|
||||
"""Response for backup deletion / purge endpoints."""
|
||||
|
||||
deleted: int = Field(description="Number of backup files deleted")
|
||||
|
||||
|
||||
class BackupContentResponse(BaseModel):
|
||||
"""Response for ``GET /api/backups/content``."""
|
||||
|
||||
content: str = Field(description="Backup file content (truncated to 100 KB)")
|
||||
name: str = Field(description="Backup file name")
|
||||
size: int = Field(description="Returned content size in characters")
|
||||
|
||||
|
||||
class BackupsCompressResponse(BaseModel):
|
||||
"""Response for ``POST /api/backups/compress``."""
|
||||
|
||||
compressed: int = Field(description="Number of backups processed")
|
||||
saved_bytes: int = Field(description="Bytes saved by gzip compression")
|
||||
dry_run: bool
|
||||
|
||||
|
||||
class BackupsAutoResponse(BaseModel):
|
||||
"""Response for ``POST /api/backups/auto``."""
|
||||
|
||||
backed_up: int = Field(description="Number of files backed up")
|
||||
since_hours: int | float = Field(description="Look-back window in hours")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# PDF
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class PdfInfoResponse(BaseModel):
|
||||
"""Response for ``GET /api/file/{vault}/pdf/info``."""
|
||||
|
||||
vault: str
|
||||
path: str
|
||||
pages: int = Field(description="Page count")
|
||||
title: str = Field(description="PDF title (metadata or filename)")
|
||||
author: str = Field(default="", description="PDF author")
|
||||
size_bytes: int = Field(description="File size in bytes")
|
||||
|
||||
model_config = ConfigDict(
|
||||
json_schema_extra={
|
||||
"example": {
|
||||
"vault": "TestVault",
|
||||
"path": "docs/rapport.pdf",
|
||||
"pages": 12,
|
||||
"title": "Rapport annuel",
|
||||
"author": "ObsiGate",
|
||||
"size_bytes": 524288,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Search & replace
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class ReplaceMatch(BaseModel):
|
||||
"""A single file affected by a find/replace operation."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
vault: str
|
||||
path: str
|
||||
title: str | None = None
|
||||
match_count: int | None = Field(default=None, description="Occurrences found (dry run)")
|
||||
replacements: int | None = Field(default=None, description="Occurrences replaced")
|
||||
preview: list[str] = Field(default_factory=list, description="Context snippets (dry run)")
|
||||
|
||||
|
||||
class ReplaceResponse(BaseModel):
|
||||
"""Response for ``POST /api/search/replace``."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
matches: list[ReplaceMatch] = Field(default_factory=list, description="Dry-run matches")
|
||||
replaced: list[ReplaceMatch] = Field(default_factory=list, description="Applied replacements")
|
||||
total_matches: int | None = Field(default=None, description="Total matches (dry run)")
|
||||
total_replacements: int = Field(description="Total replacements performed or previewed")
|
||||
dry_run: bool | None = Field(default=None, description="True when no file was written")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Vaults, attachments & settings
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class VaultStatsResponse(BaseModel):
|
||||
"""Response for ``POST /api/vaults/add`` and ``GET /api/index/reload/{vault}``."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
status: str = Field(default="ok")
|
||||
vault: str
|
||||
stats: dict[str, Any] = Field(default_factory=dict, description="Index statistics for the vault")
|
||||
|
||||
|
||||
class VaultActionResponse(BaseModel):
|
||||
"""Response for ``DELETE /api/vaults/{vault}``."""
|
||||
|
||||
status: str = Field(default="ok")
|
||||
vault: str
|
||||
|
||||
|
||||
class VaultStatusEntry(BaseModel):
|
||||
"""Per-vault status entry."""
|
||||
|
||||
file_count: int
|
||||
tag_count: int
|
||||
path: str = ""
|
||||
watching: bool = False
|
||||
|
||||
|
||||
class VaultsStatusResponse(BaseModel):
|
||||
"""Response for ``GET /api/vaults/status``."""
|
||||
|
||||
vaults: dict[str, VaultStatusEntry]
|
||||
watcher_active: bool
|
||||
sse_clients: int
|
||||
|
||||
|
||||
class AttachmentRescanResponse(BaseModel):
|
||||
"""Response for ``POST /api/attachments/rescan/{vault}``."""
|
||||
|
||||
status: str = Field(default="ok")
|
||||
vault: str
|
||||
attachment_count: int
|
||||
|
||||
|
||||
class AttachmentStatsResponse(BaseModel):
|
||||
"""Response for ``GET /api/attachments/stats``."""
|
||||
|
||||
vaults: dict[str, Any] = Field(description="Vault name → attachment statistics")
|
||||
|
||||
|
||||
class VaultSettingsResponse(BaseModel):
|
||||
"""Response for the per-vault display settings endpoints."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
hideHiddenFiles: bool = Field(default=False, description="Hide dotfiles in the tree")
|
||||
|
||||
|
||||
class AllVaultSettingsResponse(BaseModel):
|
||||
"""Response for ``GET /api/vaults/settings/all``."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
class VaultFileEntry(BaseModel):
|
||||
"""A file entry returned by the vault home listing."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
name: str
|
||||
path: str
|
||||
vault: str
|
||||
size: int = 0
|
||||
modified: float = 0
|
||||
modified_iso: str | None = None
|
||||
extension: str = ""
|
||||
rel_dir: str | None = None
|
||||
|
||||
|
||||
class VaultFilesResponse(BaseModel):
|
||||
"""Response for ``GET /api/vault/{vault}/files``."""
|
||||
|
||||
vault: str
|
||||
directory: str = ""
|
||||
recursive: bool = True
|
||||
count: int
|
||||
files: list[VaultFileEntry]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Configuration, AI keys & diagnostics
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class AppConfigResponse(BaseModel):
|
||||
"""Application configuration (``GET/POST /api/config``)."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
class AIKeysResponse(BaseModel):
|
||||
"""Masked AI provider keys (``GET /api/config/ai-keys``)."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
class AIKeyDeleteResponse(BaseModel):
|
||||
"""Response for ``DELETE /api/config/ai-keys/{provider}``."""
|
||||
|
||||
status: str = Field(default="deleted")
|
||||
key: str = Field(description="Deleted environment variable name")
|
||||
|
||||
|
||||
class AITestResponse(BaseModel):
|
||||
"""Response for ``POST /api/config/ai-keys/test`` (provider → status)."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
class AIModelsResponse(BaseModel):
|
||||
"""Response for ``GET /api/config/ai-models``."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
models: list[str] = Field(default_factory=list, description="Available model identifiers")
|
||||
source: str = Field(default="fallback", description="'live', 'fallback' or 'validation'")
|
||||
count: int | None = Field(default=None, description="Number of live models")
|
||||
error: str | None = Field(default=None, description="Provider/network error, if any")
|
||||
note: str | None = Field(default=None, description="Explanatory note when using fallback")
|
||||
capabilities: dict[str, dict[str, bool]] = Field(
|
||||
default_factory=dict,
|
||||
description="Per-model capability flags (chat, embeddings, vision, …)",
|
||||
)
|
||||
|
||||
|
||||
class DiagnosticsResponse(BaseModel):
|
||||
"""Response for ``GET /api/diagnostics``."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
index: dict[str, Any] = Field(default_factory=dict)
|
||||
inverted_index: dict[str, Any] = Field(default_factory=dict)
|
||||
config: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class DashboardVaultStat(BaseModel):
|
||||
"""Per-vault dashboard statistics."""
|
||||
|
||||
name: str
|
||||
file_count: int
|
||||
tag_count: int
|
||||
total_size_bytes: int
|
||||
|
||||
|
||||
class DashboardResponse(BaseModel):
|
||||
"""Response for ``GET /api/dashboard``."""
|
||||
|
||||
vaults: list[DashboardVaultStat]
|
||||
total_files: int
|
||||
total_tags: int
|
||||
total_size_bytes: int
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Webhooks, sharing & conflicts
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class WebhookModel(BaseModel):
|
||||
"""A configured webhook."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
id: str = Field(description="Webhook UUID")
|
||||
name: str
|
||||
url: str = Field(description="Target HTTP(S) URL")
|
||||
events: list[str] = Field(default_factory=list, description="Subscribed event types")
|
||||
secret: str | None = Field(default=None, description="HMAC-SHA256 signing secret")
|
||||
enabled: bool = True
|
||||
created_at: str | None = None
|
||||
last_fired_at: str | None = None
|
||||
|
||||
|
||||
class ShareModel(BaseModel):
|
||||
"""A public document share."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
id: str
|
||||
token: str = Field(description="Opaque share token")
|
||||
vault: str
|
||||
path: str
|
||||
url: str | None = Field(default=None, description="Relative public URL (/s/{token})")
|
||||
created_by: str | None = None
|
||||
created_at: str | None = None
|
||||
expires_at: str | None = None
|
||||
access_count: int = 0
|
||||
last_accessed: str | None = None
|
||||
|
||||
|
||||
class ConflictEntry(BaseModel):
|
||||
"""A Syncthing sync-conflict file."""
|
||||
|
||||
model_config = ConfigDict(extra="allow")
|
||||
vault: str
|
||||
path: str
|
||||
original_path: str | None = None
|
||||
|
||||
|
||||
class ConflictsResponse(BaseModel):
|
||||
"""Response for ``GET /api/conflicts``."""
|
||||
|
||||
conflicts: list[ConflictEntry]
|
||||
total: int
|
||||
|
||||
|
||||
class ConflictResolveResponse(BaseModel):
|
||||
"""Response for ``POST /api/conflicts/resolve``."""
|
||||
|
||||
status: str = Field(default="resolved")
|
||||
action: str | None = Field(default=None, description="'keep_local' or 'keep_conflict'")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# AI status & BooksLM context
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class AIProviderStatus(BaseModel):
|
||||
"""Availability of a single AI provider."""
|
||||
|
||||
available: bool
|
||||
model: str | None = Field(default=None, description="Default model when the provider is configured")
|
||||
|
||||
|
||||
class AIAutocompleteStatus(BaseModel):
|
||||
"""Status of the local Ollama autocomplete backend."""
|
||||
|
||||
available: bool = False
|
||||
server_ok: bool = False
|
||||
model_loaded: bool = False
|
||||
model: str = ""
|
||||
error: str | None = None
|
||||
|
||||
|
||||
class AIStatusResponse(BaseModel):
|
||||
"""Response for ``GET /api/ai/status``."""
|
||||
|
||||
configured: bool = Field(description="True when at least one provider has an API key")
|
||||
default_provider: str
|
||||
providers: dict[str, AIProviderStatus]
|
||||
autocomplete: AIAutocompleteStatus
|
||||
|
||||
|
||||
class BooksLMContextFile(BaseModel):
|
||||
"""A single document included in the BooksLM context."""
|
||||
|
||||
path: str
|
||||
title: str
|
||||
content: str
|
||||
type: str = Field(default="markdown", description="'markdown' (and future types)")
|
||||
|
||||
|
||||
class BooksLMContextResponse(BaseModel):
|
||||
"""Response for ``POST /api/ai/bookslm/context``."""
|
||||
|
||||
files: list[BooksLMContextFile]
|
||||
total_chars: int
|
||||
file_count: int
|
||||
directory_tree: str = ""
|
||||
max_total_chars: int = Field(default=200000, description="Configured context character limit")
|
||||
max_files: int = Field(default=200, description="Configured file-count limit")
|
||||
scope: str = Field(default="directory", description="Context scope: 'directory', 'documents' or 'general'")
|
||||
|
||||
+464
-133
@@ -1,16 +1,23 @@
|
||||
from sortedcontainers import SortedList
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
import time
|
||||
import unicodedata
|
||||
from collections import defaultdict
|
||||
from typing import List, Dict, Any, Optional, Tuple
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
from snowballstemmer import stemmer as _snowball_stemmer
|
||||
from sortedcontainers import SortedList
|
||||
|
||||
from backend import indexer as _indexer
|
||||
from backend import semantic_search as _semantic
|
||||
from backend.indexer import index
|
||||
from backend.services.regex_safety import (
|
||||
MAX_REGEX_MATCHES,
|
||||
truncate_for_regex,
|
||||
validate_regex,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("obsigate.search")
|
||||
|
||||
@@ -86,7 +93,7 @@ def normalize_text(text: str) -> str:
|
||||
return stripped.lower()
|
||||
|
||||
|
||||
def tokenize(text: str) -> List[str]:
|
||||
def tokenize(text: str) -> list[str]:
|
||||
"""Split text into normalized tokens (accent-stripped, lowercased words).
|
||||
|
||||
Args:
|
||||
@@ -101,7 +108,7 @@ def tokenize(text: str) -> List[str]:
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tag filter helper (unchanged for backward compat)
|
||||
# ---------------------------------------------------------------------------
|
||||
def _normalize_tag_filter(tag_filter: Optional[str]) -> List[str]:
|
||||
def _normalize_tag_filter(tag_filter: str | None) -> list[str]:
|
||||
"""Parse a comma-separated tag filter string into a clean list.
|
||||
|
||||
Strips whitespace and leading ``#`` from each tag.
|
||||
@@ -154,7 +161,7 @@ def _extract_snippet(content: str, query: str, context_chars: int = SNIPPET_CONT
|
||||
|
||||
def _extract_highlighted_snippet(
|
||||
content: str,
|
||||
query_terms: List[str],
|
||||
query_terms: list[str],
|
||||
context_chars: int = SNIPPET_CONTEXT_CHARS,
|
||||
max_highlights: int = MAX_SNIPPET_HIGHLIGHTS,
|
||||
) -> str:
|
||||
@@ -224,12 +231,16 @@ def _extract_regex_snippet(
|
||||
if not content or not pattern_text:
|
||||
return content[:200].strip() if content else ""
|
||||
|
||||
# BUG-025: bound the text scanned and the number of matches collected.
|
||||
content = truncate_for_regex(content)
|
||||
|
||||
try:
|
||||
validate_regex(pattern_text)
|
||||
pattern = re.compile(pattern_text, re.IGNORECASE)
|
||||
except re.error:
|
||||
except (re.error, ValueError):
|
||||
return _escape_html(content[:200].strip())
|
||||
|
||||
matches = list(pattern.finditer(content))
|
||||
matches = list(pattern.finditer(content))[:MAX_REGEX_MATCHES]
|
||||
if not matches:
|
||||
return _escape_html(content[:200].strip())
|
||||
|
||||
@@ -256,14 +267,13 @@ def _extract_regex_snippet(
|
||||
parts.append(_escape_html(snippet[prev:m.start()]))
|
||||
parts.append(f"<mark>{_escape_html(snippet[m.start():m.end()])}</mark>")
|
||||
prev = m.end()
|
||||
count += 1
|
||||
if prev < len(snippet):
|
||||
parts.append(_escape_html(snippet[prev:]))
|
||||
|
||||
return prefix + "".join(parts) + suffix
|
||||
|
||||
|
||||
def _highlight_terms(text: str, terms: List[str], max_highlights: int) -> str:
|
||||
def _highlight_terms(text: str, terms: list[str], max_highlights: int) -> str:
|
||||
"""Wrap occurrences of *terms* in *text* with ``<mark>`` tags.
|
||||
|
||||
Uses accent-normalized comparison so diacritical variants are matched.
|
||||
@@ -282,7 +292,7 @@ def _highlight_terms(text: str, terms: List[str], max_highlights: int) -> str:
|
||||
|
||||
norm = normalize_text(text)
|
||||
# Collect (start, end) spans for all term matches
|
||||
spans: List[Tuple[int, int]] = []
|
||||
spans: list[tuple[int, int]] = []
|
||||
for term in terms:
|
||||
idx = 0
|
||||
while idx < len(norm):
|
||||
@@ -297,7 +307,7 @@ def _highlight_terms(text: str, terms: List[str], max_highlights: int) -> str:
|
||||
|
||||
# Merge overlapping spans and limit count
|
||||
spans.sort()
|
||||
merged: List[Tuple[int, int]] = [spans[0]]
|
||||
merged: list[tuple[int, int]] = [spans[0]]
|
||||
for s, e in spans[1:]:
|
||||
if s <= merged[-1][1]:
|
||||
merged[-1] = (merged[-1][0], max(merged[-1][1], e))
|
||||
@@ -306,7 +316,7 @@ def _highlight_terms(text: str, terms: List[str], max_highlights: int) -> str:
|
||||
merged = merged[:max_highlights]
|
||||
|
||||
# Build result with highlights
|
||||
parts: List[str] = []
|
||||
parts: list[str] = []
|
||||
prev = 0
|
||||
for s, e in merged:
|
||||
if s > prev:
|
||||
@@ -348,17 +358,17 @@ class InvertedIndex:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.word_index: Dict[str, Dict[str, int]] = defaultdict(dict)
|
||||
self.title_index: Dict[str, List[str]] = defaultdict(list)
|
||||
self.tag_norm_map: Dict[str, str] = {}
|
||||
self.tag_prefix_index: Dict[str, List[str]] = defaultdict(list)
|
||||
self.title_norm_map: Dict[str, List[Dict[str, str]]] = defaultdict(list)
|
||||
self.word_index: dict[str, dict[str, int]] = defaultdict(dict)
|
||||
self.title_index: dict[str, list[str]] = defaultdict(list)
|
||||
self.tag_norm_map: dict[str, str] = {}
|
||||
self.tag_prefix_index: dict[str, list[str]] = defaultdict(list)
|
||||
self.title_norm_map: dict[str, list[dict[str, str]]] = defaultdict(list)
|
||||
self.doc_count: int = 0
|
||||
self.doc_info: Dict[str, Dict[str, Any]] = {}
|
||||
self.doc_vault: Dict[str, str] = {}
|
||||
self.vault_docs: Dict[str, set] = defaultdict(set)
|
||||
self.tag_docs: Dict[str, set] = defaultdict(set)
|
||||
self._sorted_tokens: "SortedList" = SortedList()
|
||||
self.doc_info: dict[str, dict[str, Any]] = {}
|
||||
self.doc_vault: dict[str, str] = {}
|
||||
self.vault_docs: dict[str, set] = defaultdict(set)
|
||||
self.tag_docs: dict[str, set] = defaultdict(set)
|
||||
self._sorted_tokens: SortedList = SortedList()
|
||||
self._ready: bool = False # True after initial build
|
||||
|
||||
def is_stale(self) -> bool:
|
||||
@@ -415,12 +425,12 @@ class InvertedIndex:
|
||||
content = file_info.get("content", "")
|
||||
full_text = (file_info.get("title", "") + " " + content)
|
||||
tokens = tokenize(full_text)
|
||||
tf: Dict[str, int] = defaultdict(int)
|
||||
tf: dict[str, int] = defaultdict(int)
|
||||
for token in tokens:
|
||||
tf[token] += 1
|
||||
# Also index stemmed forms (French stemming)
|
||||
# Compute stem frequencies in one pass instead of O(S×T)
|
||||
stem_freqs: Dict[str, int] = defaultdict(int)
|
||||
stem_freqs: dict[str, int] = defaultdict(int)
|
||||
for token, freq in tf.items():
|
||||
stemmed = stem_token(token)
|
||||
if stemmed != token: # only index stem if different
|
||||
@@ -489,12 +499,12 @@ class InvertedIndex:
|
||||
content = file_info.get("content", "")
|
||||
full_text = title + " " + content
|
||||
tokens = tokenize(full_text)
|
||||
tf: Dict[str, int] = defaultdict(int)
|
||||
tf: dict[str, int] = defaultdict(int)
|
||||
for token in tokens:
|
||||
if token:
|
||||
tf[token] += 1
|
||||
# Also compute stems in one pass
|
||||
stem_freqs: Dict[str, int] = defaultdict(int)
|
||||
stem_freqs: dict[str, int] = defaultdict(int)
|
||||
for token, freq in tf.items():
|
||||
stemmed = stem_token(token)
|
||||
if stemmed != token:
|
||||
@@ -615,7 +625,7 @@ class InvertedIndex:
|
||||
return 0.0
|
||||
return tf * self.idf(term)
|
||||
|
||||
def get_prefix_tokens(self, prefix: str, max_expansions: int = 50) -> List[str]:
|
||||
def get_prefix_tokens(self, prefix: str, max_expansions: int = 50) -> list[str]:
|
||||
"""Get all tokens starting with *prefix* using binary search.
|
||||
|
||||
Uses a pre-sorted token list for O(log V + k) lookup instead
|
||||
@@ -631,7 +641,7 @@ class InvertedIndex:
|
||||
if not prefix or not self._sorted_tokens:
|
||||
return []
|
||||
lo = self._sorted_tokens.bisect_left(prefix)
|
||||
results: List[str] = []
|
||||
results: list[str] = []
|
||||
for i in range(lo, len(self._sorted_tokens)):
|
||||
if self._sorted_tokens[i].startswith(prefix):
|
||||
results.append(self._sorted_tokens[i])
|
||||
@@ -656,6 +666,10 @@ def _on_index_change_hook(action: str, vault_name: str, path: str, file_info: di
|
||||
inv.remove_document(vault_name, path)
|
||||
except Exception as e:
|
||||
logger.warning(f"Inverted index incremental update failed ({action} {vault_name}/{path}): {e}")
|
||||
try:
|
||||
_semantic.on_index_change(action, vault_name, path, file_info)
|
||||
except Exception as e:
|
||||
logger.warning(f"Semantic index incremental update failed ({action} {vault_name}/{path}): {e}")
|
||||
|
||||
|
||||
# Register the hook with indexer (indexer is already imported at top of file)
|
||||
@@ -688,9 +702,9 @@ def get_inverted_index() -> InvertedIndex:
|
||||
def search(
|
||||
query: str,
|
||||
vault_filter: str = "all",
|
||||
tag_filter: Optional[str] = None,
|
||||
tag_filter: str | None = None,
|
||||
limit: int = DEFAULT_SEARCH_LIMIT,
|
||||
) -> List[Dict[str, Any]]:
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Full-text search across indexed vaults with relevance scoring.
|
||||
|
||||
Scoring heuristics (when a text query is provided):
|
||||
@@ -722,62 +736,99 @@ def search(
|
||||
return []
|
||||
|
||||
query_lower = query.lower()
|
||||
results: List[Dict[str, Any]] = []
|
||||
results: list[dict[str, Any]] = []
|
||||
|
||||
for vault_name, vault_data in index.items():
|
||||
if vault_filter != "all" and vault_name != vault_filter:
|
||||
inv = get_inverted_index()
|
||||
use_index = (not inv.is_stale()) and inv.doc_count > 0
|
||||
|
||||
if use_index:
|
||||
# BUG-033: retrieve candidates from the inverted index instead of
|
||||
# scanning every document. Multi-term queries require all terms
|
||||
# (a superset of exact-phrase matches), single terms use prefix
|
||||
# expansion. Falls back to a full scan while the index is building.
|
||||
if has_query:
|
||||
terms = [t for t in tokenize(query) if t]
|
||||
if not terms:
|
||||
return []
|
||||
doc_sets: list[set] = []
|
||||
for term in terms:
|
||||
term_docs: set = set(inv.word_index.get(term, {}).keys())
|
||||
if len(term) >= MIN_PREFIX_LENGTH:
|
||||
for expanded in inv.get_prefix_tokens(term):
|
||||
term_docs.update(inv.word_index.get(expanded, {}).keys())
|
||||
doc_sets.append(term_docs)
|
||||
doc_keys = set.intersection(*doc_sets) if doc_sets else set()
|
||||
else:
|
||||
doc_keys = set(inv.doc_info.keys())
|
||||
|
||||
if vault_filter != "all":
|
||||
doc_keys &= inv.vault_docs.get(vault_filter, set())
|
||||
for tag in selected_tags:
|
||||
doc_keys &= inv.tag_docs.get(tag.lower(), set())
|
||||
|
||||
candidates = [
|
||||
(inv.doc_vault[dk], inv.doc_info[dk])
|
||||
for dk in doc_keys
|
||||
if dk in inv.doc_info
|
||||
]
|
||||
else:
|
||||
candidates = [
|
||||
(vault_name, file_info)
|
||||
for vault_name, vault_data in index.items()
|
||||
if vault_filter == "all" or vault_name == vault_filter
|
||||
for file_info in vault_data["files"]
|
||||
]
|
||||
|
||||
for vault_name, file_info in candidates:
|
||||
# Tag filter: all selected tags must be present
|
||||
if selected_tags and not all(tag in file_info["tags"] for tag in selected_tags):
|
||||
continue
|
||||
|
||||
for file_info in vault_data["files"]:
|
||||
# Tag filter: all selected tags must be present
|
||||
if selected_tags and not all(tag in file_info["tags"] for tag in selected_tags):
|
||||
continue
|
||||
score = 0
|
||||
snippet = file_info.get("content_preview", "")
|
||||
|
||||
score = 0
|
||||
snippet = file_info.get("content_preview", "")
|
||||
if has_query:
|
||||
title_lower = file_info["title"].lower()
|
||||
|
||||
if has_query:
|
||||
title_lower = file_info["title"].lower()
|
||||
# Exact title match (highest weight)
|
||||
if query_lower == title_lower:
|
||||
score += 20
|
||||
# Partial title match
|
||||
elif query_lower in title_lower:
|
||||
score += 10
|
||||
|
||||
# Exact title match (highest weight)
|
||||
if query_lower == title_lower:
|
||||
score += 20
|
||||
# Partial title match
|
||||
elif query_lower in title_lower:
|
||||
score += 10
|
||||
# Path match (folder/filename relevance)
|
||||
if query_lower in file_info["path"].lower():
|
||||
score += 5
|
||||
|
||||
# Path match (folder/filename relevance)
|
||||
if query_lower in file_info["path"].lower():
|
||||
score += 5
|
||||
# Tag name match
|
||||
for tag in file_info.get("tags", []):
|
||||
if query_lower in tag.lower():
|
||||
score += 3
|
||||
break # count once per file
|
||||
|
||||
# Tag name match
|
||||
for tag in file_info.get("tags", []):
|
||||
if query_lower in tag.lower():
|
||||
score += 3
|
||||
break # count once per file
|
||||
# Content match — use cached content (no disk I/O)
|
||||
content = file_info.get("content", "")
|
||||
content_lower = content.lower()
|
||||
if query_lower in content_lower:
|
||||
# Frequency-based scoring, capped to avoid over-weighting
|
||||
occurrences = content_lower.count(query_lower)
|
||||
score += min(occurrences, 10)
|
||||
snippet = _extract_snippet(content, query)
|
||||
else:
|
||||
# Tag-only filter: all matching files get score 1
|
||||
score = 1
|
||||
|
||||
# Content match — use cached content (no disk I/O)
|
||||
content = file_info.get("content", "")
|
||||
content_lower = content.lower()
|
||||
if query_lower in content_lower:
|
||||
# Frequency-based scoring, capped to avoid over-weighting
|
||||
occurrences = content_lower.count(query_lower)
|
||||
score += min(occurrences, 10)
|
||||
snippet = _extract_snippet(content, query)
|
||||
else:
|
||||
# Tag-only filter: all matching files get score 1
|
||||
score = 1
|
||||
|
||||
if score > 0:
|
||||
results.append({
|
||||
"vault": vault_name,
|
||||
"path": file_info["path"],
|
||||
"title": file_info["title"],
|
||||
"tags": file_info["tags"],
|
||||
"score": score,
|
||||
"snippet": snippet,
|
||||
"modified": file_info["modified"],
|
||||
})
|
||||
if score > 0:
|
||||
results.append({
|
||||
"vault": vault_name,
|
||||
"path": file_info["path"],
|
||||
"title": file_info["title"],
|
||||
"tags": file_info["tags"],
|
||||
"score": score,
|
||||
"snippet": snippet,
|
||||
"modified": file_info["modified"],
|
||||
})
|
||||
|
||||
results.sort(key=lambda x: -x["score"])
|
||||
return results[:limit]
|
||||
@@ -786,7 +837,7 @@ def search(
|
||||
# ---------------------------------------------------------------------------
|
||||
# Advanced search with TF-IDF scoring
|
||||
# ---------------------------------------------------------------------------
|
||||
def _parse_advanced_query(raw_query: str) -> Dict[str, Any]:
|
||||
def _parse_advanced_query(raw_query: str) -> dict[str, Any]:
|
||||
"""Parse an advanced query string into structured filters and free text.
|
||||
|
||||
Supported operators:
|
||||
@@ -803,7 +854,7 @@ def _parse_advanced_query(raw_query: str) -> Dict[str, Any]:
|
||||
Returns:
|
||||
Dict with keys ``tags``, ``vault``, ``title``, ``path``, ``ext``, ``terms``.
|
||||
"""
|
||||
parsed: Dict[str, Any] = {
|
||||
parsed: dict[str, Any] = {
|
||||
"tags": [],
|
||||
"vault": None,
|
||||
"title": None,
|
||||
@@ -838,7 +889,7 @@ def _parse_advanced_query(raw_query: str) -> Dict[str, Any]:
|
||||
return parsed
|
||||
|
||||
|
||||
def _split_query_tokens(raw: str) -> List[str]:
|
||||
def _split_query_tokens(raw: str) -> list[str]:
|
||||
"""Split a query string respecting quoted phrases.
|
||||
|
||||
``tag:"my tag" hello world`` → ``['tag:my tag', 'hello', 'world']``
|
||||
@@ -849,7 +900,7 @@ def _split_query_tokens(raw: str) -> List[str]:
|
||||
Returns:
|
||||
List of token strings.
|
||||
"""
|
||||
tokens: List[str] = []
|
||||
tokens: list[str] = []
|
||||
i = 0
|
||||
n = len(raw)
|
||||
while i < n:
|
||||
@@ -890,25 +941,27 @@ def _split_query_tokens(raw: str) -> List[str]:
|
||||
|
||||
def _passes_search_filters(
|
||||
file_info: dict,
|
||||
query_terms: List[str],
|
||||
query_terms_raw: List[str],
|
||||
query_terms: list[str],
|
||||
query_terms_raw: list[str],
|
||||
raw_query: str,
|
||||
case_sensitive: bool,
|
||||
whole_word: bool,
|
||||
regex: bool,
|
||||
include_paths: Optional[str],
|
||||
exclude_paths: Optional[str],
|
||||
include_paths: str | None,
|
||||
exclude_paths: str | None,
|
||||
) -> bool:
|
||||
"""Post-filter a candidate by case-sensitive, whole-word, regex, and path filters."""
|
||||
title = file_info.get("title", "")
|
||||
content = file_info.get("content", "")
|
||||
path = file_info.get("path", "")
|
||||
search_text = f"{title} {content}"
|
||||
# BUG-025: cap the text scanned by a user-supplied regex.
|
||||
search_text = truncate_for_regex(f"{title} {content}")
|
||||
search_text_norm = normalize_text(search_text)
|
||||
|
||||
# --- Regex mode ---
|
||||
if regex and raw_query:
|
||||
try:
|
||||
validate_regex(raw_query)
|
||||
flags = 0 if case_sensitive else re.IGNORECASE
|
||||
if whole_word:
|
||||
pattern = re.compile(rf"\b{raw_query}\b", flags)
|
||||
@@ -916,7 +969,7 @@ def _passes_search_filters(
|
||||
pattern = re.compile(raw_query, flags)
|
||||
if not pattern.search(search_text):
|
||||
return False
|
||||
except re.error:
|
||||
except (re.error, ValueError):
|
||||
return False
|
||||
return _passes_path_filters(path, include_paths, exclude_paths)
|
||||
|
||||
@@ -937,7 +990,7 @@ def _passes_search_filters(
|
||||
return _passes_path_filters(path, include_paths, exclude_paths)
|
||||
|
||||
|
||||
def _passes_path_filters(path: str, include: Optional[str], exclude: Optional[str]) -> bool:
|
||||
def _passes_path_filters(path: str, include: str | None, exclude: str | None) -> bool:
|
||||
"""Check if a file path passes include/exclude glob patterns."""
|
||||
import fnmatch
|
||||
if include:
|
||||
@@ -951,19 +1004,160 @@ def _passes_path_filters(path: str, include: Optional[str], exclude: Optional[st
|
||||
return True
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Date and size filter helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _parse_date_range(raw: str | None) -> tuple | None:
|
||||
"""Parse a date range filter string.
|
||||
|
||||
Supported formats:
|
||||
- ">2024-01-01" → after (inclusive)
|
||||
- "<2024-06-01" → before (inclusive)
|
||||
- "2024-01-01..2024-06-01" → between (inclusive)
|
||||
- "<7d" → within last 7 days
|
||||
- ">30d" → older than 30 days
|
||||
- "YYYY-MM-DD" → exact date match (created/modified on that day)
|
||||
Returns (min_ts, max_ts) in Unix timestamps, or None if unparseable.
|
||||
"""
|
||||
if not raw:
|
||||
return None
|
||||
raw = raw.strip()
|
||||
|
||||
# Relative: <7d, >30d
|
||||
rel_match = re.match(r'^([<>])(\d+)([dh])$', raw)
|
||||
if rel_match:
|
||||
op, num, unit = rel_match.groups()
|
||||
n = int(num)
|
||||
seconds = n * 3600 if unit == 'h' else n * 86400
|
||||
now = time.time()
|
||||
if op == '<':
|
||||
return (now - seconds, now)
|
||||
else:
|
||||
return (0, now - seconds)
|
||||
|
||||
# Range: date1..date2
|
||||
if '..' in raw:
|
||||
parts = raw.split('..', 1)
|
||||
t1 = _parse_date_to_ts(parts[0].strip())
|
||||
t2 = _parse_date_to_ts(parts[1].strip())
|
||||
if t1 is not None and t2 is not None:
|
||||
return (min(t1, t2), max(t1, t2))
|
||||
return None
|
||||
|
||||
# Comparator: >date, <date
|
||||
if raw.startswith('>'):
|
||||
ts = _parse_date_to_ts(raw[1:].strip())
|
||||
return (ts, 9999999999) if ts is not None else None
|
||||
if raw.startswith('<'):
|
||||
ts = _parse_date_to_ts(raw[1:].strip())
|
||||
return (0, ts) if ts is not None else None
|
||||
|
||||
# Exact date
|
||||
ts = _parse_date_to_ts(raw)
|
||||
if ts is not None:
|
||||
return (ts, ts + 86400) # whole day
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _parse_date_to_ts(s: str) -> float | None:
|
||||
"""Parse a date string to Unix timestamp. Supports YYYY-MM-DD."""
|
||||
s = s.strip()
|
||||
for fmt in ('%Y-%m-%d', '%Y/%m/%d', '%d/%m/%Y'):
|
||||
try:
|
||||
from datetime import datetime as dt_mod
|
||||
return dt_mod.strptime(s, fmt).timestamp()
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def _matches_date_range(file_ts: float | None, date_range: tuple) -> bool:
|
||||
"""Check if a file timestamp falls within the given range."""
|
||||
if file_ts is None:
|
||||
return False
|
||||
return date_range[0] <= file_ts <= date_range[1]
|
||||
|
||||
|
||||
def _parse_size_range(raw: str | None) -> tuple | None:
|
||||
"""Parse a size range filter string.
|
||||
|
||||
Supported formats:
|
||||
- ">1MB", ">500KB", ">1GB" → larger than
|
||||
- "<100KB" → smaller than
|
||||
- "500KB..2MB" → between
|
||||
Returns (min_bytes, max_bytes), or None if unparseable.
|
||||
"""
|
||||
if not raw:
|
||||
return None
|
||||
raw = raw.strip()
|
||||
|
||||
def _parse_size(s: str) -> int | None:
|
||||
s = s.strip().upper()
|
||||
mult = 1
|
||||
if s.endswith('GB'):
|
||||
mult = 1024 * 1024 * 1024
|
||||
s = s[:-2]
|
||||
elif s.endswith('MB'):
|
||||
mult = 1024 * 1024
|
||||
s = s[:-2]
|
||||
elif s.endswith('KB'):
|
||||
mult = 1024
|
||||
s = s[:-2]
|
||||
elif s.endswith('B') or s.endswith('O'):
|
||||
mult = 1
|
||||
s = s[:-1]
|
||||
try:
|
||||
return int(float(s) * mult)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
if '..' in raw:
|
||||
parts = raw.split('..', 1)
|
||||
lo = _parse_size(parts[0])
|
||||
hi = _parse_size(parts[1])
|
||||
if lo is not None and hi is not None:
|
||||
return (min(lo, hi), max(lo, hi))
|
||||
return None
|
||||
|
||||
if raw.startswith('>'):
|
||||
lo = _parse_size(raw[1:])
|
||||
return (lo, 10**15) if lo is not None else None
|
||||
if raw.startswith('<'):
|
||||
hi = _parse_size(raw[1:])
|
||||
return (0, hi) if hi is not None else None
|
||||
|
||||
# Exact: try parsing as a number (bytes) or with unit
|
||||
exact = _parse_size(raw)
|
||||
if exact is not None:
|
||||
return (exact, exact * 2) # fuzzy match
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _matches_size_range(file_size: int, size_range: tuple) -> bool:
|
||||
"""Check if a file size falls within the given range."""
|
||||
return size_range[0] <= file_size <= size_range[1]
|
||||
|
||||
|
||||
def advanced_search(
|
||||
query: str,
|
||||
vault_filter: str = "all",
|
||||
tag_filter: Optional[str] = None,
|
||||
tag_filter: str | None = None,
|
||||
limit: int = ADVANCED_SEARCH_DEFAULT_LIMIT,
|
||||
offset: int = 0,
|
||||
sort_by: str = "relevance",
|
||||
case_sensitive: bool = False,
|
||||
whole_word: bool = False,
|
||||
regex: bool = False,
|
||||
include_paths: Optional[str] = None,
|
||||
exclude_paths: Optional[str] = None,
|
||||
) -> Dict[str, Any]:
|
||||
include_paths: str | None = None,
|
||||
exclude_paths: str | None = None,
|
||||
created: str | None = None,
|
||||
modified: str | None = None,
|
||||
size: str | None = None,
|
||||
semantic: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Advanced full-text search with TF-IDF scoring, facets, and pagination.
|
||||
|
||||
Uses the inverted index for O(k × postings) candidate retrieval instead
|
||||
@@ -982,13 +1176,21 @@ def advanced_search(
|
||||
limit: Max results per page.
|
||||
offset: Pagination offset.
|
||||
sort_by: ``"relevance"`` or ``"modified"``.
|
||||
semantic: When True, fuse the TF-IDF ranking with the semantic
|
||||
(embedding) ranking via Reciprocal Rank Fusion and expose a
|
||||
``semantic_score`` per result.
|
||||
|
||||
Returns:
|
||||
Dict with ``results``, ``total``, ``offset``, ``limit``, ``facets``,
|
||||
``query_time_ms``.
|
||||
``query_time_ms`` and ``semantic_available``.
|
||||
"""
|
||||
t0 = time.monotonic()
|
||||
query = query.strip() if query else ""
|
||||
|
||||
# BUG-025: reject oversized / catastrophic regex patterns up front.
|
||||
if regex and query:
|
||||
validate_regex(query)
|
||||
|
||||
parsed = _parse_advanced_query(query)
|
||||
|
||||
# Merge explicit tag_filter with parsed tag: operators
|
||||
@@ -1043,46 +1245,73 @@ def advanced_search(
|
||||
# ------------------------------------------------------------------
|
||||
# Step 2: Apply filters on candidate set
|
||||
# ------------------------------------------------------------------
|
||||
if effective_vault != "all":
|
||||
candidates &= inv.vault_docs.get(effective_vault, set())
|
||||
date_range_created = _parse_date_range(created or parsed.get("created"))
|
||||
date_range_modified = _parse_date_range(modified or parsed.get("modified"))
|
||||
size_range = _parse_size_range(size or parsed.get("size"))
|
||||
|
||||
if all_tags and has_terms:
|
||||
for t in all_tags:
|
||||
candidates &= inv.tag_docs.get(t.lower(), set())
|
||||
def _apply_metadata_filters(docs: set) -> set:
|
||||
"""Restrict a document-key set to the query's metadata filters."""
|
||||
if effective_vault != "all":
|
||||
docs &= inv.vault_docs.get(effective_vault, set())
|
||||
|
||||
if parsed["title"]:
|
||||
norm_title_filter = normalize_text(parsed["title"])
|
||||
candidates = {
|
||||
dk for dk in candidates
|
||||
if norm_title_filter in normalize_text(inv.doc_info[dk].get("title", ""))
|
||||
}
|
||||
if all_tags:
|
||||
for t in all_tags:
|
||||
docs &= inv.tag_docs.get(t.lower(), set())
|
||||
|
||||
if parsed["path"]:
|
||||
norm_path_filter = normalize_text(parsed["path"])
|
||||
candidates = {
|
||||
dk for dk in candidates
|
||||
if norm_path_filter in normalize_text(inv.doc_info[dk].get("path", ""))
|
||||
}
|
||||
if parsed["title"]:
|
||||
norm_title_filter = normalize_text(parsed["title"])
|
||||
docs = {
|
||||
dk for dk in docs
|
||||
if norm_title_filter in normalize_text(inv.doc_info[dk].get("title", ""))
|
||||
}
|
||||
|
||||
if parsed["ext"]:
|
||||
ext_filter = parsed["ext"]
|
||||
candidates = {
|
||||
dk for dk in candidates
|
||||
if (
|
||||
inv.doc_info[dk].get("path", "").rsplit("/", 1)[-1].lower() == ext_filter
|
||||
or inv.doc_info[dk].get("path", "").rsplit("/", 1)[-1].lower().endswith(f".{ext_filter}")
|
||||
)
|
||||
}
|
||||
if parsed["path"]:
|
||||
norm_path_filter = normalize_text(parsed["path"])
|
||||
docs = {
|
||||
dk for dk in docs
|
||||
if norm_path_filter in normalize_text(inv.doc_info[dk].get("path", ""))
|
||||
}
|
||||
|
||||
if parsed["ext"]:
|
||||
ext_filter = parsed["ext"]
|
||||
docs = {
|
||||
dk for dk in docs
|
||||
if (
|
||||
inv.doc_info[dk].get("path", "").rsplit("/", 1)[-1].lower() == ext_filter
|
||||
or inv.doc_info[dk].get("path", "").rsplit("/", 1)[-1].lower().endswith(f".{ext_filter}")
|
||||
)
|
||||
}
|
||||
|
||||
if date_range_created:
|
||||
docs = {
|
||||
dk for dk in docs
|
||||
if _matches_date_range(inv.doc_info[dk].get("created"), date_range_created)
|
||||
}
|
||||
|
||||
if date_range_modified:
|
||||
docs = {
|
||||
dk for dk in docs
|
||||
if _matches_date_range(inv.doc_info[dk].get("modified"), date_range_modified)
|
||||
}
|
||||
|
||||
if size_range:
|
||||
docs = {
|
||||
dk for dk in docs
|
||||
if _matches_size_range(inv.doc_info[dk].get("size", 0), size_range)
|
||||
}
|
||||
return docs
|
||||
|
||||
candidates = _apply_metadata_filters(candidates)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Step 3: Score only the candidates (not all N documents)
|
||||
# ------------------------------------------------------------------
|
||||
scored_results: List[Tuple[float, Dict[str, Any]]] = []
|
||||
facet_tags: Dict[str, int] = defaultdict(int)
|
||||
facet_vaults: Dict[str, int] = defaultdict(int)
|
||||
scored_results: list[tuple[float, dict[str, Any]]] = []
|
||||
facet_tags: dict[str, int] = defaultdict(int)
|
||||
facet_vaults: dict[str, int] = defaultdict(int)
|
||||
|
||||
# Pre-compute prefix expansions once per term (avoid repeated binary search)
|
||||
prefix_expansions: Dict[str, List[str]] = {}
|
||||
prefix_expansions: dict[str, list[str]] = {}
|
||||
if has_terms:
|
||||
for term in query_terms:
|
||||
if len(term) >= MIN_PREFIX_LENGTH:
|
||||
@@ -1151,16 +1380,22 @@ def advanced_search(
|
||||
"title": file_info["title"],
|
||||
"tags": file_info.get("tags", []),
|
||||
"score": round(score, 4),
|
||||
"semantic_score": 0.0,
|
||||
"snippet": snippet,
|
||||
"modified": file_info.get("modified", ""),
|
||||
"extension": file_info.get("extension", file_info.get("path", "").rsplit(".", 1)[-1] if "." in file_info.get("path", "") else ""),
|
||||
}
|
||||
scored_results.append((score, result))
|
||||
|
||||
# Facets
|
||||
facet_vaults[vault_name] = facet_vaults.get(vault_name, 0) + 1
|
||||
for tag in file_info.get("tags", []):
|
||||
facet_tags[tag] = facet_tags.get(tag, 0) + 1
|
||||
# ------------------------------------------------------------------
|
||||
# Step 4: Optional semantic fusion (RRF with the TF-IDF ranking)
|
||||
# ------------------------------------------------------------------
|
||||
semantic_available = _semantic.get_semantic_index().is_ready()
|
||||
if semantic and has_terms and not regex:
|
||||
scored_results = _fuse_semantic_results(
|
||||
scored_results, query, effective_vault, inv, _apply_metadata_filters,
|
||||
include_paths, exclude_paths, limit,
|
||||
)
|
||||
|
||||
# Sort
|
||||
if sort_by == "modified":
|
||||
@@ -1168,6 +1403,12 @@ def advanced_search(
|
||||
else:
|
||||
scored_results.sort(key=lambda x: -x[0])
|
||||
|
||||
# Facets are recomputed from the final result set (covers semantic-only docs)
|
||||
for _, result in scored_results:
|
||||
facet_vaults[result["vault"]] = facet_vaults.get(result["vault"], 0) + 1
|
||||
for tag in result.get("tags", []):
|
||||
facet_tags[tag] = facet_tags.get(tag, 0) + 1
|
||||
|
||||
total = len(scored_results)
|
||||
page = scored_results[offset: offset + limit]
|
||||
elapsed_ms = round((time.monotonic() - t0) * 1000, 1)
|
||||
@@ -1182,9 +1423,99 @@ def advanced_search(
|
||||
"vaults": dict(sorted(facet_vaults.items(), key=lambda x: -x[1])),
|
||||
},
|
||||
"query_time_ms": elapsed_ms,
|
||||
"semantic_available": semantic_available,
|
||||
}
|
||||
|
||||
|
||||
def _fuse_semantic_results(
|
||||
scored_results: list[tuple[float, dict[str, Any]]],
|
||||
query: str,
|
||||
vault_filter: str,
|
||||
inv: InvertedIndex,
|
||||
apply_metadata_filters: Callable[[set], set],
|
||||
include_paths: str | None,
|
||||
exclude_paths: str | None,
|
||||
limit: int,
|
||||
) -> list[tuple[float, dict[str, Any]]]:
|
||||
"""Fuse the TF-IDF ranking with the semantic ranking using RRF.
|
||||
|
||||
Documents found only by the semantic engine are materialized from the
|
||||
inverted index metadata (with a plain, non-highlighted snippet). The
|
||||
returned tuples carry the fused score, and every result dict gets a
|
||||
``semantic_score`` (cosine similarity, 0.0 when absent).
|
||||
|
||||
Args:
|
||||
scored_results: Existing ``(tfidf_score, result_dict)`` tuples.
|
||||
query: Raw free-text query.
|
||||
vault_filter: Effective vault filter.
|
||||
inv: Inverted index (document metadata source).
|
||||
apply_metadata_filters: Callable restricting a doc-key set to the
|
||||
query's tag/title/path/ext/date/size filters.
|
||||
include_paths: Include glob patterns (or None).
|
||||
exclude_paths: Exclude glob patterns (or None).
|
||||
limit: Requested page size (drives how many semantic hits to fetch).
|
||||
|
||||
Returns:
|
||||
New ``(fused_score, result_dict)`` list (unsorted).
|
||||
"""
|
||||
sem_hits = _semantic.semantic_search_docs(query, vault_filter=vault_filter, top_k=max(limit * 5, 200))
|
||||
if not sem_hits:
|
||||
return scored_results
|
||||
|
||||
# Semantic candidates must satisfy the same metadata + path filters.
|
||||
semantic_universe = apply_metadata_filters(set(inv.doc_info.keys()))
|
||||
semantic_universe = {
|
||||
dk for dk in semantic_universe
|
||||
if _passes_path_filters(inv.doc_info[dk].get("path", ""), include_paths, exclude_paths)
|
||||
}
|
||||
|
||||
sem_scores: dict[str, float] = {}
|
||||
sem_ranked: list[str] = []
|
||||
for doc_key, similarity in sem_hits:
|
||||
if doc_key not in semantic_universe:
|
||||
continue
|
||||
sem_scores[doc_key] = similarity
|
||||
sem_ranked.append(doc_key)
|
||||
|
||||
if not sem_ranked:
|
||||
return scored_results
|
||||
|
||||
existing: dict[str, dict[str, Any]] = {}
|
||||
lexical_ranked: list[str] = []
|
||||
for _, lex_result in sorted(scored_results, key=lambda item: -item[0]):
|
||||
key = f"{lex_result['vault']}::{lex_result['path']}"
|
||||
existing[key] = lex_result
|
||||
lexical_ranked.append(key)
|
||||
|
||||
fused = _semantic.rrf_fuse([lexical_ranked, sem_ranked])
|
||||
|
||||
merged: list[tuple[float, dict[str, Any]]] = []
|
||||
for doc_key, fused_score in fused.items():
|
||||
result = existing.get(doc_key)
|
||||
if result is None:
|
||||
file_info = inv.doc_info.get(doc_key)
|
||||
if file_info is None:
|
||||
continue
|
||||
content = file_info.get("content", "")
|
||||
result = {
|
||||
"vault": inv.doc_vault[doc_key],
|
||||
"path": file_info["path"],
|
||||
"title": file_info["title"],
|
||||
"tags": file_info.get("tags", []),
|
||||
"score": 0.0,
|
||||
"semantic_score": 0.0,
|
||||
"snippet": _escape_html(content[:200].strip()) if content else "",
|
||||
"modified": file_info.get("modified", ""),
|
||||
"extension": file_info.get(
|
||||
"extension",
|
||||
file_info.get("path", "").rsplit(".", 1)[-1] if "." in file_info.get("path", "") else "",
|
||||
),
|
||||
}
|
||||
result["semantic_score"] = round(sem_scores.get(doc_key, 0.0), 4)
|
||||
merged.append((fused_score, result))
|
||||
return merged
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Suggestion helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -1192,7 +1523,7 @@ def suggest_titles(
|
||||
prefix: str,
|
||||
vault_filter: str = "all",
|
||||
limit: int = SUGGEST_LIMIT,
|
||||
) -> List[Dict[str, str]]:
|
||||
) -> list[dict[str, str]]:
|
||||
"""Suggest file titles matching a prefix (accent-insensitive).
|
||||
|
||||
Args:
|
||||
@@ -1208,7 +1539,7 @@ def suggest_titles(
|
||||
|
||||
inv = get_inverted_index()
|
||||
norm_prefix = normalize_text(prefix)
|
||||
results: List[Dict[str, str]] = []
|
||||
results: list[dict[str, str]] = []
|
||||
seen: set = set()
|
||||
|
||||
for norm_title, entries in inv.title_norm_map.items():
|
||||
@@ -1230,7 +1561,7 @@ def suggest_tags(
|
||||
prefix: str,
|
||||
vault_filter: str = "all",
|
||||
limit: int = SUGGEST_LIMIT,
|
||||
) -> List[Dict[str, Any]]:
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Suggest tags matching a prefix (accent-insensitive).
|
||||
|
||||
Args:
|
||||
@@ -1248,7 +1579,7 @@ def suggest_tags(
|
||||
norm_prefix = normalize_text(prefix)
|
||||
all_tag_counts = get_all_tags(vault_filter)
|
||||
|
||||
matches: List[Dict[str, Any]] = []
|
||||
matches: list[dict[str, Any]] = []
|
||||
for tag, count in all_tag_counts.items():
|
||||
norm_tag = normalize_text(tag)
|
||||
if norm_prefix in norm_tag:
|
||||
@@ -1262,7 +1593,7 @@ def suggest_tags(
|
||||
# ---------------------------------------------------------------------------
|
||||
# Backward-compatible tag aggregation (unchanged API)
|
||||
# ---------------------------------------------------------------------------
|
||||
def get_all_tags(vault_filter: Optional[str] = None) -> Dict[str, int]:
|
||||
def get_all_tags(vault_filter: str | None = None) -> dict[str, int]:
|
||||
"""Aggregate tag counts across vaults, sorted by descending count.
|
||||
|
||||
Args:
|
||||
@@ -1271,7 +1602,7 @@ def get_all_tags(vault_filter: Optional[str] = None) -> Dict[str, int]:
|
||||
Returns:
|
||||
Dict mapping tag names to their total occurrence count.
|
||||
"""
|
||||
merged: Dict[str, int] = {}
|
||||
merged: dict[str, int] = {}
|
||||
for vault_name, vault_data in index.items():
|
||||
if vault_filter and vault_filter != "all" and vault_name != vault_filter:
|
||||
continue
|
||||
|
||||
@@ -13,8 +13,8 @@ Patterns detected:
|
||||
- Connection strings with passwords
|
||||
"""
|
||||
|
||||
import re
|
||||
import logging
|
||||
import re
|
||||
|
||||
logger = logging.getLogger("obsigate.redactor")
|
||||
|
||||
|
||||
@@ -0,0 +1,612 @@
|
||||
"""ObsiGate — Semantic search: embeddings, vector store and hybrid retrieval.
|
||||
|
||||
This module adds a *semantic* layer on top of the existing TF-IDF search. Each
|
||||
document is split into overlapping chunks, each chunk is converted into a dense
|
||||
vector, and queries are matched by cosine similarity. Results are combined with
|
||||
the lexical ranking through Reciprocal Rank Fusion (RRF).
|
||||
|
||||
Design goals
|
||||
------------
|
||||
* **Zero mandatory dependency.** ``sentence-transformers`` (local model),
|
||||
``numpy`` and ``faiss`` are *optional*. They are imported lazily and, when
|
||||
missing, the module falls back to a deterministic pure-Python hashing embedder
|
||||
and a pure-Python cosine store. The feature therefore degrades gracefully and
|
||||
the default CI (which only installs ``backend/requirements.txt``) keeps working.
|
||||
* **Plug-in providers.** Embeddings can come from the local
|
||||
``all-MiniLM-L6-v2`` model, from an OpenAI-compatible ``/embeddings`` endpoint
|
||||
(configured via env vars), or from the deterministic fallback.
|
||||
* **Incremental.** The index is updated document-by-document from the indexer
|
||||
change hook (file watcher + API mutations), never rebuilt on each search.
|
||||
|
||||
Optional extras are listed in ``backend/requirements-semantic.txt``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
from abc import ABC, abstractmethod
|
||||
from collections import Counter
|
||||
from itertools import pairwise
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.semantic")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Constants
|
||||
# ---------------------------------------------------------------------------
|
||||
EMBEDDING_DIM = 384 # all-MiniLM-L6-v2 output dimension
|
||||
CHUNK_TOKENS = 512 # target chunk size (whitespace tokens)
|
||||
CHUNK_OVERLAP_TOKENS = 64 # overlap between consecutive chunks
|
||||
DEFAULT_TOP_K = 200 # max documents returned by a semantic query
|
||||
RRF_K = 60 # Reciprocal Rank Fusion smoothing constant
|
||||
MAX_QUERY_CHARS = 2000 # guard against pathological queries
|
||||
|
||||
_WORD_RE = re.compile(r"[\w]+", re.UNICODE)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tokenization / chunking
|
||||
# ---------------------------------------------------------------------------
|
||||
def _simple_tokens(text: str) -> list[str]:
|
||||
"""Split *text* into lowercase word tokens (keeps accents)."""
|
||||
return _WORD_RE.findall(text.lower())
|
||||
|
||||
|
||||
def chunk_text(
|
||||
text: str,
|
||||
chunk_tokens: int = CHUNK_TOKENS,
|
||||
overlap: int = CHUNK_OVERLAP_TOKENS,
|
||||
) -> list[str]:
|
||||
"""Split *text* into overlapping windows of roughly *chunk_tokens* words.
|
||||
|
||||
Args:
|
||||
text: Raw document text.
|
||||
chunk_tokens: Target number of whitespace tokens per chunk.
|
||||
overlap: Number of tokens shared by two consecutive chunks.
|
||||
|
||||
Returns:
|
||||
A list of chunk strings. Empty input yields an empty list.
|
||||
"""
|
||||
if not text or not text.strip():
|
||||
return []
|
||||
if chunk_tokens <= 0:
|
||||
chunk_tokens = CHUNK_TOKENS
|
||||
overlap = max(0, min(overlap, chunk_tokens - 1))
|
||||
|
||||
words = text.split()
|
||||
if len(words) <= chunk_tokens:
|
||||
return [" ".join(words)]
|
||||
|
||||
step = max(1, chunk_tokens - overlap)
|
||||
chunks: list[str] = []
|
||||
for start in range(0, len(words), step):
|
||||
window = words[start:start + chunk_tokens]
|
||||
if not window:
|
||||
break
|
||||
chunks.append(" ".join(window))
|
||||
if start + chunk_tokens >= len(words):
|
||||
break
|
||||
return chunks
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Embedding providers
|
||||
# ---------------------------------------------------------------------------
|
||||
class EmbeddingProvider(ABC):
|
||||
"""Base class for embedding backends."""
|
||||
|
||||
name: str = "base"
|
||||
|
||||
def __init__(self, dimension: int = EMBEDDING_DIM) -> None:
|
||||
self.dimension = dimension
|
||||
|
||||
@abstractmethod
|
||||
def encode(self, texts: list[str]) -> list[list[float]]:
|
||||
"""Return one L2-normalized vector per input text."""
|
||||
|
||||
def encode_one(self, text: str) -> list[float]:
|
||||
"""Convenience wrapper returning the vector for a single text."""
|
||||
vectors = self.encode([text])
|
||||
return vectors[0] if vectors else [0.0] * self.dimension
|
||||
|
||||
|
||||
class HashEmbeddingProvider(EmbeddingProvider):
|
||||
"""Deterministic, dependency-free hashing embedder.
|
||||
|
||||
This is a *lexical* fallback: it hashes word unigrams, word bigrams and
|
||||
character trigrams into fixed-size signed buckets (the "hashing trick"),
|
||||
then L2-normalizes the result. It captures shared vocabulary and
|
||||
morphological variants (``backup``/``backups``), so it already improves
|
||||
recall over exact TF-IDF matching, but it does not understand synonyms the
|
||||
way a real transformer model does.
|
||||
"""
|
||||
|
||||
name = "hash"
|
||||
|
||||
def _add_feature(self, vec: list[float], key: str, weight: float) -> None:
|
||||
digest = hashlib.blake2b(key.encode("utf-8"), digest_size=8).digest()
|
||||
h = int.from_bytes(digest, "big")
|
||||
idx = h % self.dimension
|
||||
sign = 1.0 if (h >> 63) & 1 else -1.0
|
||||
vec[idx] += sign * weight
|
||||
|
||||
def _encode_one(self, text: str) -> list[float]:
|
||||
vec = [0.0] * self.dimension
|
||||
tokens = _simple_tokens(text)
|
||||
if not tokens:
|
||||
return vec
|
||||
|
||||
tf = Counter(tokens)
|
||||
for token, count in tf.items():
|
||||
weight = 1.0 + math.log(count)
|
||||
self._add_feature(vec, "w:" + token, weight)
|
||||
for gram in _char_ngrams(token, 3):
|
||||
self._add_feature(vec, "g:" + gram, weight * 0.5)
|
||||
|
||||
for first, second in pairwise(tokens):
|
||||
self._add_feature(vec, "b:" + first + "_" + second, 0.5)
|
||||
|
||||
norm = math.sqrt(sum(v * v for v in vec))
|
||||
if norm > 0.0:
|
||||
vec = [v / norm for v in vec]
|
||||
return vec
|
||||
|
||||
def encode(self, texts: list[str]) -> list[list[float]]:
|
||||
return [self._encode_one(t or "") for t in texts]
|
||||
|
||||
|
||||
def _char_ngrams(token: str, n: int) -> list[str]:
|
||||
"""Return padded character n-grams for *token* (bounded to avoid blow-up)."""
|
||||
if len(token) < n:
|
||||
return [token]
|
||||
if len(token) > 24:
|
||||
token = token[:24]
|
||||
return [token[i:i + n] for i in range(len(token) - n + 1)]
|
||||
|
||||
|
||||
class SentenceTransformerProvider(EmbeddingProvider):
|
||||
"""Local ``all-MiniLM-L6-v2`` embeddings via ``sentence-transformers``."""
|
||||
|
||||
name = "sentence-transformers"
|
||||
|
||||
def __init__(self, model_name: str = "all-MiniLM-L6-v2") -> None:
|
||||
super().__init__(EMBEDDING_DIM)
|
||||
self.model_name = model_name
|
||||
self._model: Any | None = None
|
||||
|
||||
@staticmethod
|
||||
def is_available() -> bool:
|
||||
try:
|
||||
import sentence_transformers # noqa: F401
|
||||
except Exception:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _get_model(self) -> Any:
|
||||
if self._model is None:
|
||||
from sentence_transformers import SentenceTransformer
|
||||
|
||||
self._model = SentenceTransformer(self.model_name)
|
||||
return self._model
|
||||
|
||||
def encode(self, texts: list[str]) -> list[list[float]]:
|
||||
if not texts:
|
||||
return []
|
||||
model = self._get_model()
|
||||
vectors = model.encode(texts, normalize_embeddings=True)
|
||||
return [[float(x) for x in vec] for vec in vectors]
|
||||
|
||||
|
||||
class RemoteEmbeddingProvider(EmbeddingProvider):
|
||||
"""OpenAI-compatible ``/embeddings`` endpoint (API key based)."""
|
||||
|
||||
name = "remote"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url: str,
|
||||
api_key: str,
|
||||
model: str = "text-embedding-3-small",
|
||||
dimension: int = EMBEDDING_DIM,
|
||||
) -> None:
|
||||
super().__init__(dimension)
|
||||
self.base_url = base_url.rstrip("/")
|
||||
self.api_key = api_key
|
||||
self.model = model
|
||||
|
||||
def encode(self, texts: list[str]) -> list[list[float]]:
|
||||
if not texts:
|
||||
return []
|
||||
import httpx
|
||||
|
||||
response = httpx.post(
|
||||
f"{self.base_url}/embeddings",
|
||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||
json={"model": self.model, "input": texts},
|
||||
timeout=30.0,
|
||||
)
|
||||
response.raise_for_status()
|
||||
payload = response.json()
|
||||
data = sorted(payload.get("data", []), key=lambda item: item.get("index", 0))
|
||||
return [self._normalize([float(x) for x in item["embedding"]]) for item in data]
|
||||
|
||||
@staticmethod
|
||||
def _normalize(vec: list[float]) -> list[float]:
|
||||
norm = math.sqrt(sum(v * v for v in vec))
|
||||
if norm > 0.0:
|
||||
return [v / norm for v in vec]
|
||||
return vec
|
||||
|
||||
|
||||
_provider: EmbeddingProvider | None = None
|
||||
_provider_lock = threading.Lock()
|
||||
|
||||
|
||||
def _build_provider() -> EmbeddingProvider:
|
||||
"""Select the best available provider (respecting ``OBSIGATE_EMBEDDING_PROVIDER``)."""
|
||||
requested = os.getenv("OBSIGATE_EMBEDDING_PROVIDER", "auto").strip().lower()
|
||||
|
||||
if requested in ("auto", "local", "sentence-transformers") and SentenceTransformerProvider.is_available():
|
||||
model = os.getenv("OBSIGATE_EMBEDDING_MODEL", "all-MiniLM-L6-v2")
|
||||
return SentenceTransformerProvider(model)
|
||||
|
||||
remote_key = os.getenv("OBSIGATE_EMBEDDING_API_KEY", "")
|
||||
remote_url = os.getenv("OBSIGATE_EMBEDDING_BASE_URL", "")
|
||||
if requested in ("auto", "remote") and remote_key and remote_url:
|
||||
model = os.getenv("OBSIGATE_EMBEDDING_MODEL", "text-embedding-3-small")
|
||||
dim = int(os.getenv("OBSIGATE_EMBEDDING_DIM", str(EMBEDDING_DIM)))
|
||||
return RemoteEmbeddingProvider(remote_url, remote_key, model, dim)
|
||||
|
||||
if requested == "remote":
|
||||
logger.warning(
|
||||
"OBSIGATE_EMBEDDING_PROVIDER=remote but OBSIGATE_EMBEDDING_API_KEY/BASE_URL missing; using hash fallback"
|
||||
)
|
||||
return HashEmbeddingProvider()
|
||||
|
||||
|
||||
def get_embedding_provider() -> EmbeddingProvider:
|
||||
"""Return the cached embedding provider (built on first use)."""
|
||||
global _provider
|
||||
with _provider_lock:
|
||||
if _provider is None:
|
||||
_provider = _build_provider()
|
||||
logger.info("Semantic embedding provider: %s (dim=%d)", _provider.name, _provider.dimension)
|
||||
return _provider
|
||||
|
||||
|
||||
def reset_embedding_provider() -> None:
|
||||
"""Forget the cached provider (used by tests and config reloads)."""
|
||||
global _provider
|
||||
with _provider_lock:
|
||||
_provider = None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Vector store
|
||||
# ---------------------------------------------------------------------------
|
||||
class VectorStore:
|
||||
"""In-memory vector store with optional numpy / faiss acceleration.
|
||||
|
||||
Vectors are always kept as Python lists (source of truth). A numpy matrix
|
||||
and/or a faiss ``IndexFlatIP`` are built lazily and invalidated on mutation.
|
||||
All vectors are expected to be L2-normalized, so the inner product equals
|
||||
the cosine similarity.
|
||||
"""
|
||||
|
||||
def __init__(self, dimension: int = EMBEDDING_DIM) -> None:
|
||||
self.dimension = dimension
|
||||
self._keys: list[str] = []
|
||||
self._chunks: list[str] = []
|
||||
self._vectors: list[list[float]] = []
|
||||
self._dirty = True
|
||||
self._numpy: Any | None = None
|
||||
self._numpy_checked = False
|
||||
self._matrix: Any | None = None
|
||||
self._faiss: Any | None = None
|
||||
self._faiss_checked = False
|
||||
self._faiss_index: Any | None = None
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._vectors)
|
||||
|
||||
def clear(self) -> None:
|
||||
self._keys = []
|
||||
self._chunks = []
|
||||
self._vectors = []
|
||||
self._dirty = True
|
||||
|
||||
def add(self, key: str, chunk: str, vector: list[float]) -> None:
|
||||
self._keys.append(key)
|
||||
self._chunks.append(chunk)
|
||||
self._vectors.append(vector)
|
||||
self._dirty = True
|
||||
|
||||
def remove_document(self, key: str) -> None:
|
||||
"""Remove every chunk belonging to *key*."""
|
||||
kept = [(k, c, v) for k, c, v in zip(self._keys, self._chunks, self._vectors) if k != key]
|
||||
if len(kept) == len(self._vectors):
|
||||
return
|
||||
self._keys = [k for k, _, _ in kept]
|
||||
self._chunks = [c for _, c, _ in kept]
|
||||
self._vectors = [v for _, _, v in kept]
|
||||
self._dirty = True
|
||||
|
||||
# -- optional accelerators -------------------------------------------------
|
||||
def _get_numpy(self) -> Any | None:
|
||||
if not self._numpy_checked:
|
||||
self._numpy_checked = True
|
||||
try:
|
||||
import numpy as np
|
||||
|
||||
self._numpy = np
|
||||
except Exception:
|
||||
self._numpy = None
|
||||
return self._numpy
|
||||
|
||||
def _get_faiss(self) -> Any | None:
|
||||
if not self._faiss_checked:
|
||||
self._faiss_checked = True
|
||||
try:
|
||||
import faiss
|
||||
|
||||
self._faiss = faiss
|
||||
except Exception:
|
||||
self._faiss = None
|
||||
return self._faiss
|
||||
|
||||
def _rebuild_accelerators(self) -> None:
|
||||
self._dirty = False
|
||||
np = self._get_numpy()
|
||||
if np is None or not self._vectors:
|
||||
self._matrix = None
|
||||
self._faiss_index = None
|
||||
return
|
||||
self._matrix = np.asarray(self._vectors, dtype="float32")
|
||||
faiss = self._get_faiss()
|
||||
if faiss is not None:
|
||||
index = faiss.IndexFlatIP(self.dimension)
|
||||
index.add(self._matrix)
|
||||
self._faiss_index = index
|
||||
else:
|
||||
self._faiss_index = None
|
||||
|
||||
def search(self, query_vector: list[float], top_k: int = DEFAULT_TOP_K) -> list[tuple[str, float]]:
|
||||
"""Return ``(doc_key, cosine_similarity)`` pairs sorted by similarity."""
|
||||
if not self._vectors:
|
||||
return []
|
||||
top_k = max(1, min(top_k, len(self._vectors)))
|
||||
if self._dirty:
|
||||
self._rebuild_accelerators()
|
||||
|
||||
np = self._get_numpy()
|
||||
if np is not None and self._faiss_index is not None and self._matrix is not None:
|
||||
query = np.asarray([query_vector], dtype="float32")
|
||||
scores, indices = self._faiss_index.search(query, top_k)
|
||||
return [
|
||||
(self._keys[int(idx)], float(score))
|
||||
for score, idx in zip(scores[0], indices[0])
|
||||
if idx >= 0
|
||||
]
|
||||
|
||||
if np is not None and self._matrix is not None:
|
||||
query = np.asarray(query_vector, dtype="float32")
|
||||
scores = self._matrix @ query
|
||||
order = np.argsort(scores)[::-1][:top_k]
|
||||
return [(self._keys[int(i)], float(scores[int(i)])) for i in order]
|
||||
|
||||
scored = [(self._keys[i], _dot(self._vectors[i], query_vector)) for i in range(len(self._vectors))]
|
||||
scored.sort(key=lambda item: item[1], reverse=True)
|
||||
return scored[:top_k]
|
||||
|
||||
def chunk_of(self, index: int) -> str:
|
||||
"""Return the stored chunk text at *index* (used by diagnostics/tests)."""
|
||||
return self._chunks[index]
|
||||
|
||||
|
||||
def _dot(a: list[float], b: list[float]) -> float:
|
||||
"""Dot product for two equal-length vectors."""
|
||||
return sum(x * y for x, y in zip(a, b))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Reciprocal Rank Fusion
|
||||
# ---------------------------------------------------------------------------
|
||||
def rrf_fuse(rankings: list[list[str]], k: int = RRF_K) -> dict[str, float]:
|
||||
"""Fuse several ranked key lists into a single score map.
|
||||
|
||||
``score(key) = Σ_rankings 1 / (k + rank(key))`` where ``rank`` is
|
||||
1-based. Documents ranked highly by several methods rise to the top.
|
||||
|
||||
Args:
|
||||
rankings: Ordered lists of document keys (best first).
|
||||
k: RRF smoothing constant.
|
||||
|
||||
Returns:
|
||||
Mapping ``doc_key -> fused score`` (insertion order is unspecified).
|
||||
"""
|
||||
scores: dict[str, float] = {}
|
||||
for ranking in rankings:
|
||||
seen: set[str] = set()
|
||||
for rank, key in enumerate(ranking):
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
scores[key] = scores.get(key, 0.0) + 1.0 / (k + rank + 1)
|
||||
return scores
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Semantic index
|
||||
# ---------------------------------------------------------------------------
|
||||
class SemanticIndex:
|
||||
"""Holds document chunk embeddings and answers similarity queries."""
|
||||
|
||||
def __init__(self, provider: EmbeddingProvider | None = None) -> None:
|
||||
self.provider = provider
|
||||
self.store = VectorStore(provider.dimension if provider else EMBEDDING_DIM)
|
||||
self.doc_keys: set[str] = set()
|
||||
self._ready = False
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def is_ready(self) -> bool:
|
||||
"""Return True once a full rebuild has completed."""
|
||||
return self._ready
|
||||
|
||||
def is_stale(self) -> bool:
|
||||
"""Alias used by callers that check index freshness."""
|
||||
return not self._ready
|
||||
|
||||
def _ensure_provider(self) -> EmbeddingProvider:
|
||||
if self.provider is None:
|
||||
self.provider = get_embedding_provider()
|
||||
self.store = VectorStore(self.provider.dimension)
|
||||
return self.provider
|
||||
|
||||
@staticmethod
|
||||
def _document_text(file_info: dict[str, Any]) -> str:
|
||||
title = file_info.get("title", "") or ""
|
||||
content = file_info.get("content", "") or ""
|
||||
return (title + "\n\n" + content).strip()
|
||||
|
||||
def _embed_document(self, doc_key: str, file_info: dict[str, Any]) -> None:
|
||||
text = self._document_text(file_info)
|
||||
if not text:
|
||||
return
|
||||
provider = self._ensure_provider()
|
||||
chunks = chunk_text(text)
|
||||
if not chunks:
|
||||
return
|
||||
vectors = provider.encode(chunks)
|
||||
for chunk, vector in zip(chunks, vectors):
|
||||
self.store.add(doc_key, chunk, vector)
|
||||
self.doc_keys.add(doc_key)
|
||||
|
||||
def rebuild(self) -> None:
|
||||
"""Rebuild the whole index from the global in-memory index."""
|
||||
from backend.indexer import index
|
||||
|
||||
provider = self._ensure_provider()
|
||||
with self._lock:
|
||||
self.store = VectorStore(provider.dimension)
|
||||
self.doc_keys = set()
|
||||
for vault_name, vault_data in index.items():
|
||||
for file_info in vault_data.get("files", []):
|
||||
doc_key = f"{vault_name}::{file_info.get('path', '')}"
|
||||
try:
|
||||
self._embed_document(doc_key, file_info)
|
||||
except Exception as exc:
|
||||
logger.warning("Semantic embedding failed for %s: %s", doc_key, exc)
|
||||
self._ready = True
|
||||
logger.info(
|
||||
"Semantic index built: %d documents, %d chunks (provider=%s)",
|
||||
len(self.doc_keys),
|
||||
len(self.store),
|
||||
provider.name,
|
||||
)
|
||||
|
||||
def add_document(self, vault_name: str, path: str, file_info: dict[str, Any]) -> None:
|
||||
"""Add or refresh a single document (no-op until the index is ready)."""
|
||||
if not self._ready or not file_info:
|
||||
return
|
||||
doc_key = f"{vault_name}::{path}"
|
||||
with self._lock:
|
||||
self.store.remove_document(doc_key)
|
||||
self.doc_keys.discard(doc_key)
|
||||
try:
|
||||
self._embed_document(doc_key, file_info)
|
||||
except Exception as exc:
|
||||
logger.warning("Semantic embedding failed for %s: %s", doc_key, exc)
|
||||
|
||||
def remove_document(self, vault_name: str, path: str) -> None:
|
||||
"""Remove a single document (no-op until the index is ready)."""
|
||||
if not self._ready:
|
||||
return
|
||||
doc_key = f"{vault_name}::{path}"
|
||||
with self._lock:
|
||||
self.store.remove_document(doc_key)
|
||||
self.doc_keys.discard(doc_key)
|
||||
|
||||
def search(
|
||||
self,
|
||||
query: str,
|
||||
vault_filter: str = "all",
|
||||
top_k: int = DEFAULT_TOP_K,
|
||||
) -> list[tuple[str, float]]:
|
||||
"""Return ``(doc_key, best_chunk_similarity)`` pairs, best first."""
|
||||
if not self._ready or not query or not query.strip():
|
||||
return []
|
||||
provider = self._ensure_provider()
|
||||
query_vector = provider.encode_one(query[:MAX_QUERY_CHARS])
|
||||
hits = self.store.search(query_vector, top_k=max(top_k * 4, top_k))
|
||||
best: dict[str, float] = {}
|
||||
for doc_key, score in hits:
|
||||
if vault_filter != "all" and not doc_key.startswith(vault_filter + "::"):
|
||||
continue
|
||||
if doc_key not in best or score > best[doc_key]:
|
||||
best[doc_key] = score
|
||||
ordered = sorted(best.items(), key=lambda item: item[1], reverse=True)
|
||||
return ordered[:top_k]
|
||||
|
||||
|
||||
_semantic_index: SemanticIndex | None = None
|
||||
_index_lock = threading.Lock()
|
||||
|
||||
|
||||
def get_semantic_index() -> SemanticIndex:
|
||||
"""Return the process-wide semantic index (created on first access)."""
|
||||
global _semantic_index
|
||||
with _index_lock:
|
||||
if _semantic_index is None:
|
||||
_semantic_index = SemanticIndex()
|
||||
return _semantic_index
|
||||
|
||||
|
||||
def reset_semantic_index() -> None:
|
||||
"""Drop the singleton index (tests)."""
|
||||
global _semantic_index
|
||||
with _index_lock:
|
||||
_semantic_index = None
|
||||
|
||||
|
||||
def init_semantic_index() -> None:
|
||||
"""Force a full semantic index build. Called after ``build_index`` on startup."""
|
||||
from backend.indexer import index
|
||||
|
||||
if any(vdata.get("files") for vdata in index.values()):
|
||||
get_semantic_index().rebuild()
|
||||
|
||||
|
||||
def on_index_change(action: str, vault_name: str, path: str, file_info: dict[str, Any]) -> None:
|
||||
"""Incremental hook registered with the indexer change notifier."""
|
||||
index_obj = get_semantic_index()
|
||||
if action == "add" and file_info:
|
||||
index_obj.add_document(vault_name, path, file_info)
|
||||
elif action == "remove":
|
||||
index_obj.remove_document(vault_name, path)
|
||||
|
||||
|
||||
def semantic_search_docs(
|
||||
query: str,
|
||||
vault_filter: str = "all",
|
||||
top_k: int = DEFAULT_TOP_K,
|
||||
) -> list[tuple[str, float]]:
|
||||
"""Convenience wrapper around :meth:`SemanticIndex.search`."""
|
||||
return get_semantic_index().search(query, vault_filter=vault_filter, top_k=top_k)
|
||||
|
||||
|
||||
def semantic_status() -> dict[str, Any]:
|
||||
"""Return provider/index diagnostics for the API and the UI."""
|
||||
index_obj = get_semantic_index()
|
||||
provider = index_obj.provider or get_embedding_provider()
|
||||
return {
|
||||
"available": index_obj.is_ready(),
|
||||
"provider": provider.name,
|
||||
"dimension": provider.dimension,
|
||||
"documents": len(index_obj.doc_keys),
|
||||
"chunks": len(index_obj.store),
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
"""Backup inspection services shared by REST routes and the AI tool layer.
|
||||
|
||||
Single source of truth for locating, listing and diffing the timestamped
|
||||
backups created before each file mutation. The write-side (creating a backup)
|
||||
stays in the route layer; only the read/inspection logic lives here so both
|
||||
``/api/file/{vault}/backups``, ``/api/file/{vault}/diff`` and the tools
|
||||
``list_backups`` / ``diff_backup`` behave identically.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import difflib
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.services.paths import resolve_safe_path
|
||||
from backend.services.vaults import get_vault_root
|
||||
|
||||
logger = logging.getLogger("obsigate.services.backups")
|
||||
|
||||
# Default number of backups kept per file (matches ``max_backups_per_file``).
|
||||
DEFAULT_MAX_BACKUPS = 10
|
||||
|
||||
|
||||
def _default_max_backups() -> int:
|
||||
"""Read ``max_backups_per_file`` from app config (lazy, best-effort)."""
|
||||
try:
|
||||
from backend.main import _load_config
|
||||
|
||||
return int(_load_config().get("max_backups_per_file", DEFAULT_MAX_BACKUPS))
|
||||
except Exception: # pragma: no cover - config unavailable
|
||||
return DEFAULT_MAX_BACKUPS
|
||||
|
||||
|
||||
def get_backup_dir(vault_name: str, relative_path: str) -> Path:
|
||||
"""Return the directory where backups for a specific file are stored.
|
||||
|
||||
Resolves relative backup paths against the SPECIFIC vault's directory,
|
||||
matching the backup-creation logic exactly.
|
||||
"""
|
||||
from backend.indexer import get_vault_data
|
||||
|
||||
backup_root = Path(os.environ.get("OBSIGATE_BACKUP_DIR", ".obsigate-backup"))
|
||||
if not backup_root.is_absolute():
|
||||
vault_data = get_vault_data(vault_name)
|
||||
if vault_data:
|
||||
backup_root = Path(vault_data["path"]) / backup_root
|
||||
return backup_root / vault_name / Path(relative_path).parent
|
||||
|
||||
|
||||
def create_backup(
|
||||
file_path: Path,
|
||||
vault_name: str,
|
||||
relative_path: str,
|
||||
*,
|
||||
max_backups: int | None = None,
|
||||
) -> Path | None:
|
||||
"""Create a timestamped backup of a file before it is modified.
|
||||
|
||||
Backups are stored as ``{backup_root}/{vault}/{relative_path}.{ts}.bak``.
|
||||
Missing or unreadable files are skipped silently (returns ``None``) so a
|
||||
backup failure never blocks the caller's mutation.
|
||||
|
||||
Args:
|
||||
file_path: Absolute path of the file to back up.
|
||||
vault_name: Name of the vault the file belongs to.
|
||||
relative_path: Vault-relative path (used to mirror the tree).
|
||||
max_backups: Number of backups to keep per file. ``None`` reads
|
||||
``max_backups_per_file`` from the app config.
|
||||
|
||||
Returns:
|
||||
The created backup path, or ``None`` when skipped.
|
||||
"""
|
||||
try:
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
logger.debug(f"Backup skipped: file not found {file_path}")
|
||||
return None
|
||||
|
||||
backup_dir = get_backup_dir(vault_name, relative_path)
|
||||
backup_dir.mkdir(parents=True, exist_ok=True)
|
||||
backup_path = backup_dir / f"{file_path.name}.{int(time.time())}.bak"
|
||||
shutil.copy2(file_path, backup_path)
|
||||
logger.info(f"Backup created: {relative_path} -> {backup_path}")
|
||||
|
||||
keep = max_backups if max_backups is not None else _default_max_backups()
|
||||
all_backups = sorted(
|
||||
[
|
||||
f
|
||||
for f in backup_dir.iterdir()
|
||||
if f.is_file() and f.name.startswith(file_path.name + ".") and f.name.endswith(".bak")
|
||||
],
|
||||
key=lambda f: f.stat().st_mtime,
|
||||
reverse=True,
|
||||
)
|
||||
for old in all_backups[keep:]:
|
||||
old.unlink()
|
||||
logger.debug(f"Auto-cleanup: removed old backup {old.name}")
|
||||
return backup_path
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to backup {relative_path} (vault={vault_name}): {e}", exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
def list_backup_files(vault_name: str, relative_path: str) -> list[dict[str, Any]]:
|
||||
"""List all backup files for a vault file, sorted newest first.
|
||||
|
||||
Backup filename format: ``{original_filename}.{timestamp}.bak``.
|
||||
|
||||
Returns a list of dicts with ``timestamp``, ``datetime``, ``size`` and
|
||||
``filename``. Missing or unreadable backup directories yield an empty list.
|
||||
"""
|
||||
backup_dir = get_backup_dir(vault_name, relative_path)
|
||||
if not backup_dir.exists():
|
||||
return []
|
||||
|
||||
original_name = Path(relative_path).name
|
||||
prefix = original_name + "."
|
||||
backups: list[dict[str, Any]] = []
|
||||
|
||||
try:
|
||||
dir_entries = list(backup_dir.iterdir())
|
||||
except PermissionError:
|
||||
logger.warning(f"Permission denied reading backup dir: {backup_dir}")
|
||||
return []
|
||||
except OSError as e:
|
||||
logger.error(f"Error reading backup dir {backup_dir}: {e}")
|
||||
return []
|
||||
|
||||
for f in dir_entries:
|
||||
try:
|
||||
if not f.is_file():
|
||||
continue
|
||||
name = f.name
|
||||
if not name.startswith(prefix) or not name.endswith(".bak"):
|
||||
continue
|
||||
ts_part = name[len(prefix):-len(".bak")]
|
||||
try:
|
||||
ts = int(ts_part)
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
try:
|
||||
dt = datetime.fromtimestamp(ts, tz=timezone.utc).isoformat()
|
||||
except (OSError, OverflowError, ValueError) as ts_err:
|
||||
logger.warning(f"Skipping backup with invalid timestamp {ts}: {ts_err}")
|
||||
continue
|
||||
st = f.stat()
|
||||
backups.append({
|
||||
"timestamp": ts,
|
||||
"datetime": dt,
|
||||
"size": st.st_size,
|
||||
"filename": name,
|
||||
})
|
||||
except Exception as entry_err:
|
||||
logger.warning(f"Skipping unreadable backup entry {f}: {entry_err}")
|
||||
continue
|
||||
|
||||
backups.sort(key=lambda b: b["timestamp"], reverse=True)
|
||||
return backups
|
||||
|
||||
|
||||
def diff_backup(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
version: int,
|
||||
compare_with: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Generate a unified diff between a backup version and another version or the current file.
|
||||
|
||||
Args:
|
||||
vault_name: Name of the vault.
|
||||
path: Relative path of the file within the vault.
|
||||
version: Timestamp of the backup used as the old/left side.
|
||||
compare_with: Optional timestamp of another backup as the new/right
|
||||
side. If omitted, the current file on disk is used.
|
||||
|
||||
Returns:
|
||||
Dict with ``vault``, ``path``, ``version``, ``compare_with``, ``diff``,
|
||||
``left_content`` and ``right_content``.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) when the file or a backup is missing,
|
||||
``read_error`` (500) when a file cannot be read.
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
original_name = Path(path).name
|
||||
|
||||
def _read_backup(ts: int) -> tuple[str, str]:
|
||||
backup_dir = get_backup_dir(vault_name, path)
|
||||
backup_path = backup_dir / f"{original_name}.{ts}.bak"
|
||||
if not backup_path.exists():
|
||||
raise ServiceError(
|
||||
f"Backup version {ts} not found for {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path, "version": ts},
|
||||
)
|
||||
try:
|
||||
content = backup_path.read_text(encoding="utf-8", errors="replace")
|
||||
except Exception as e:
|
||||
raise ServiceError(
|
||||
f"Failed to read backup {ts}: {e}",
|
||||
code="read_error",
|
||||
status=500,
|
||||
details={"vault": vault_name, "path": path, "version": ts},
|
||||
) from e
|
||||
dt = datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
|
||||
return content, f"{path}@{dt}"
|
||||
|
||||
try:
|
||||
left_content, left_label = _read_backup(version)
|
||||
|
||||
if compare_with is not None:
|
||||
right_content, right_label = _read_backup(compare_with)
|
||||
else:
|
||||
try:
|
||||
right_content = file_path.read_text(encoding="utf-8", errors="replace")
|
||||
except Exception as e:
|
||||
raise ServiceError(
|
||||
f"Failed to read current file: {e}",
|
||||
code="read_error",
|
||||
status=500,
|
||||
details={"vault": vault_name, "path": path},
|
||||
) from e
|
||||
right_label = f"{path} (current)"
|
||||
|
||||
left_lines = left_content.splitlines(keepends=True)
|
||||
right_lines = right_content.splitlines(keepends=True)
|
||||
|
||||
diff_lines = list(difflib.unified_diff(
|
||||
left_lines, right_lines,
|
||||
fromfile=left_label, tofile=right_label,
|
||||
))
|
||||
|
||||
return {
|
||||
"vault": vault_name,
|
||||
"path": path,
|
||||
"version": version,
|
||||
"compare_with": compare_with,
|
||||
"diff": "".join(diff_lines),
|
||||
"left_content": left_content,
|
||||
"right_content": right_content,
|
||||
}
|
||||
except ServiceError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating diff for {vault_name}/{path}: {type(e).__name__}: {e}", exc_info=True)
|
||||
raise ServiceError(f"Erreur lors de la génération du diff: {e!s}", code="read_error", status=500) from e
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Domain errors shared by the reusable service layer.
|
||||
|
||||
Services are transport-agnostic: they raise :class:`ServiceError` carrying a
|
||||
stable ``code`` and an HTTP ``status`` hint. The REST layer maps it to an
|
||||
``HTTPException`` (via the global handler in ``backend.main``) while the tool
|
||||
layer maps it to a :class:`backend.tools.context.ToolError`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
class ServiceError(Exception):
|
||||
"""Base error for the shared business-logic services."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str,
|
||||
*,
|
||||
code: str = "service_error",
|
||||
status: int = 400,
|
||||
details: dict[str, Any] | None = None,
|
||||
):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.code = code
|
||||
self.status = status
|
||||
self.details = details or {}
|
||||
@@ -0,0 +1,86 @@
|
||||
"""File reading services shared by REST routes and the AI tool layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.services.paths import resolve_safe_path
|
||||
from backend.services.vaults import get_vault_root
|
||||
|
||||
logger = logging.getLogger("obsigate.services.files")
|
||||
|
||||
|
||||
def read_raw_file(vault_name: str, path: str) -> dict[str, Any]:
|
||||
"""Return the raw text content of a vault file (no redaction)."""
|
||||
root = get_vault_root(vault_name)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
try:
|
||||
raw = file_path.read_text(encoding="utf-8", errors="replace")
|
||||
except PermissionError as e:
|
||||
logger.error(f"Permission denied reading raw file {path}: {e}")
|
||||
raise ServiceError(f"Permission denied: cannot read file {path}", code="permission_denied", status=403) from e
|
||||
except UnicodeDecodeError:
|
||||
try:
|
||||
raw = file_path.read_bytes().decode("utf-8", errors="replace")
|
||||
except Exception as e:
|
||||
logger.error(f"Error reading binary raw file {path}: {e}")
|
||||
raise ServiceError(f"Cannot read file: {e!s}", code="read_error", status=500) from e
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error reading raw file {path}: {e}")
|
||||
raise ServiceError(f"Error reading file: {e!s}", code="read_error", status=500) from e
|
||||
|
||||
return {"vault": vault_name, "path": path, "raw": raw}
|
||||
|
||||
|
||||
def read_file_text(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
*,
|
||||
redact: bool = True,
|
||||
max_bytes: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return a vault file's text content, optionally redacted and size-capped.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404), ``file_too_large`` (413) or a read
|
||||
error (500).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
target = resolve_safe_path(root, path)
|
||||
|
||||
if not target.exists() or not target.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
size = target.stat().st_size
|
||||
if max_bytes is not None and size > max_bytes:
|
||||
raise ServiceError(
|
||||
f"File too large ({size} bytes > {max_bytes})",
|
||||
code="file_too_large",
|
||||
status=413,
|
||||
details={"vault": vault_name, "path": path, "size": size},
|
||||
)
|
||||
|
||||
content = target.read_text(encoding="utf-8", errors="replace")
|
||||
|
||||
if redact:
|
||||
from backend.secret_redactor import redact_file_content
|
||||
|
||||
content = redact_file_content(content, path)
|
||||
|
||||
return {"vault": vault_name, "path": path, "size": size, "content": content}
|
||||
@@ -0,0 +1,199 @@
|
||||
"""Graph data services shared by the REST route and the AI tool layer.
|
||||
|
||||
Builds the nodes/edges structure consumed by the graph view. The route
|
||||
``/api/graph/{vault}`` and the tool ``get_graph`` both delegate here so the
|
||||
graph semantics (parent/child edges, wikilinks, tag filter) stay identical.
|
||||
Permission checks are the caller's responsibility.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from backend.indexer import SUPPORTED_EXTENSIONS, index
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.services.paths import resolve_safe_path
|
||||
from backend.services.vaults import get_vault_root
|
||||
|
||||
_WIKILINK_PATTERN = re.compile(r"\[\[([^\]|#]+)(?:[|#][^\]]+)?\]\]")
|
||||
|
||||
|
||||
def get_graph(
|
||||
vault_name: str,
|
||||
path: str = "",
|
||||
depth: int = 1,
|
||||
scope: str = "directory",
|
||||
tag: str = "",
|
||||
) -> dict[str, Any]:
|
||||
"""Return graph data (nodes and edges) for a vault or directory.
|
||||
|
||||
Args:
|
||||
vault_name: Name of the vault.
|
||||
path: Relative directory path to focus on (empty = root).
|
||||
depth: Expansion depth (0 = only direct children, 1-3 = deeper).
|
||||
scope: ``"directory"`` for a subtree, ``"full"`` for the whole vault.
|
||||
tag: Optional tag filter (only files with this tag appear).
|
||||
|
||||
Returns:
|
||||
Dict with ``vault``, ``path``, ``scope``, ``nodes`` and ``edges``.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) when the vault or path is missing.
|
||||
"""
|
||||
from backend.vault_settings import get_vault_setting
|
||||
|
||||
vault_root = get_vault_root(vault_name)
|
||||
target = resolve_safe_path(vault_root, path) if path else vault_root.resolve()
|
||||
|
||||
if not target.exists():
|
||||
raise ServiceError(
|
||||
f"Path not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
nodes: list[dict[str, Any]] = []
|
||||
edges: list[dict[str, Any]] = []
|
||||
node_ids: set[str] = set()
|
||||
|
||||
def _add_node(name: str, ntype: str, npath: str, size: int = 0,
|
||||
tags: list[str] | None = None, incoming: int = 0, outgoing: int = 0) -> str:
|
||||
nid = f"{vault_name}:{npath}"
|
||||
if nid not in node_ids:
|
||||
node_ids.add(nid)
|
||||
nodes.append({
|
||||
"id": nid, "name": name, "type": ntype, "path": npath,
|
||||
"size": size, "tags": tags or [],
|
||||
"incoming_count": incoming, "outgoing_count": outgoing,
|
||||
})
|
||||
return nid
|
||||
|
||||
def _add_edge(source: str, target_id: str, relation: str) -> None:
|
||||
edges.append({"source": source, "target": target_id, "relation": relation})
|
||||
|
||||
settings = get_vault_setting(vault_name) or {}
|
||||
hide_hidden = settings.get("hideHiddenFiles", False)
|
||||
|
||||
# Build tag index from the in-memory index for fast lookups.
|
||||
_tag_index: dict[str, list[str]] = {}
|
||||
for doc_key, info in index.items():
|
||||
vn, fp = doc_key.split("::", 1) if "::" in doc_key else ("", "")
|
||||
if vn == vault_name:
|
||||
for t in info.get("tags", []):
|
||||
_tag_index.setdefault(t.lower(), []).append(fp)
|
||||
|
||||
if scope == "full":
|
||||
target = vault_root.resolve()
|
||||
effective_depth = depth if depth > 0 else 2
|
||||
else:
|
||||
target = resolve_safe_path(vault_root, path) if path else vault_root.resolve()
|
||||
effective_depth = depth
|
||||
|
||||
if not target.exists():
|
||||
raise ServiceError(
|
||||
f"Path not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
focus_name = path.split("/")[-1] if path else vault_name
|
||||
focus_type = "directory" if path else "vault"
|
||||
focus_id = _add_node(focus_name, focus_type, path)
|
||||
|
||||
def _walk_dir(dir_path: Path, parent_id: str, current_depth: int) -> None:
|
||||
if current_depth > effective_depth:
|
||||
return
|
||||
try:
|
||||
for entry in sorted(dir_path.iterdir(), key=lambda e: (not e.is_dir(), e.name.lower())):
|
||||
if hide_hidden and entry.name.startswith("."):
|
||||
continue
|
||||
rel = str(entry.relative_to(vault_root)).replace("\\", "/")
|
||||
|
||||
if tag and entry.is_file():
|
||||
file_tags = [t.lower() for t in _tag_index.get(rel, [])]
|
||||
if tag.lower() not in file_tags:
|
||||
continue
|
||||
|
||||
if entry.is_dir():
|
||||
did = _add_node(entry.name, "directory", rel)
|
||||
_add_edge(parent_id, did, "parent")
|
||||
if current_depth < effective_depth:
|
||||
_walk_dir(entry, did, current_depth + 1)
|
||||
elif entry.suffix.lower() in SUPPORTED_EXTENSIONS or entry.name.lower() in ("dockerfile", "makefile"):
|
||||
file_tags = _tag_index.get(rel, [])
|
||||
fid = _add_node(entry.name, "file", rel, entry.stat().st_size, tags=file_tags)
|
||||
_add_edge(parent_id, fid, "parent")
|
||||
except PermissionError:
|
||||
pass
|
||||
|
||||
if target.is_dir():
|
||||
_walk_dir(target, focus_id, 0)
|
||||
elif target.is_file():
|
||||
_walk_dir(target.parent, focus_id, 0)
|
||||
|
||||
_add_wikilink_edges(nodes, edges, vault_name)
|
||||
|
||||
edge_counts: dict[str, dict[str, int]] = {}
|
||||
for node in nodes:
|
||||
edge_counts[node["id"]] = {"incoming": 0, "outgoing": 0}
|
||||
for edge in edges:
|
||||
if edge["relation"] in ("wikilink", "backlink"):
|
||||
src = edge["source"]
|
||||
tgt = edge["target"]
|
||||
if src in edge_counts:
|
||||
edge_counts[src]["outgoing"] += 1
|
||||
if tgt in edge_counts:
|
||||
edge_counts[tgt]["incoming"] += 1
|
||||
for node in nodes:
|
||||
counts = edge_counts.get(node["id"], {"incoming": 0, "outgoing": 0})
|
||||
node["incoming_count"] = counts["incoming"]
|
||||
node["outgoing_count"] = counts["outgoing"]
|
||||
|
||||
return {"vault": vault_name, "path": path, "scope": scope, "nodes": nodes, "edges": edges}
|
||||
|
||||
|
||||
def _add_wikilink_edges(nodes: list[dict[str, Any]], edges: list[dict[str, Any]], vault_name: str) -> None:
|
||||
"""Add edges for wikilinks between markdown files in the current graph scope."""
|
||||
file_nodes = [n for n in nodes if n["type"] == "file" and n["path"].endswith(".md")]
|
||||
if len(file_nodes) < 2:
|
||||
return
|
||||
|
||||
path_to_id = {n["path"]: n["id"] for n in file_nodes}
|
||||
existing = {(e["source"], e["target"]) for e in edges}
|
||||
|
||||
for node in file_nodes:
|
||||
vault_data = index.get(vault_name)
|
||||
if not vault_data:
|
||||
continue
|
||||
file_entry = None
|
||||
for f in vault_data.get("files", []):
|
||||
if f["path"] == node["path"]:
|
||||
file_entry = f
|
||||
break
|
||||
if not file_entry:
|
||||
continue
|
||||
|
||||
content = file_entry.get("content", "")
|
||||
if not content:
|
||||
continue
|
||||
|
||||
for match in _WIKILINK_PATTERN.finditer(content):
|
||||
target = match.group(1).strip()
|
||||
target_lower = target.lower()
|
||||
if not target_lower.endswith(".md"):
|
||||
target_lower += ".md"
|
||||
|
||||
for target_path, target_id in path_to_id.items():
|
||||
if target_id == node["id"]:
|
||||
continue
|
||||
target_name = target_path.rsplit("/", 1)[-1].lower()
|
||||
if target_name == target_lower or target_path.lower() == target_lower:
|
||||
edge_key = tuple(sorted([node["id"], target_id]))
|
||||
if edge_key not in existing and (edge_key[1], edge_key[0]) not in existing:
|
||||
edges.append({"source": node["id"], "target": target_id, "relation": "wikilink"})
|
||||
existing.add((node["id"], target_id))
|
||||
break
|
||||
@@ -0,0 +1,811 @@
|
||||
"""File and directory mutation services shared by REST routes and the AI tool layer.
|
||||
|
||||
Single source of truth for the write-side operations (create, edit, append,
|
||||
rename, move, delete, restore, find/replace). Every function performs the
|
||||
anti path-traversal check, the read-only guard and an automatic backup before
|
||||
any destructive change, then returns a JSON-friendly result dict.
|
||||
|
||||
Index refresh, SSE broadcasts, webhooks and audit logging stay in the route /
|
||||
agent layers: these services are synchronous and side-effect free apart from
|
||||
the filesystem mutation (plus the backup).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from backend.services.backups import create_backup, get_backup_dir
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.services.paths import resolve_safe_path
|
||||
from backend.services.vaults import get_vault_root
|
||||
|
||||
logger = logging.getLogger("obsigate.services.mutations")
|
||||
|
||||
# Skeleton injected into empty ``.excalidraw`` files (mirrors the route logic).
|
||||
_EXCALIDRAW_SKELETON = (
|
||||
'{"type":"excalidraw","version":2,"elements":[],'
|
||||
'"appState":{"viewBackgroundColor":"#ffffff"},"files":{}}'
|
||||
)
|
||||
|
||||
|
||||
def _rel(root: Path, path: Path) -> str:
|
||||
"""Return *path* relative to *root* as a POSIX-style string."""
|
||||
return str(path.relative_to(root)).replace("\\", "/")
|
||||
|
||||
|
||||
def _ensure_writable(root: Path) -> None:
|
||||
"""Raise ``read_only`` (403) when the vault root is not writable."""
|
||||
if not os.access(root, os.W_OK):
|
||||
raise ServiceError("Vault is read-only", code="read_only", status=403)
|
||||
|
||||
|
||||
def _validate_extension(file_path: Path, *, allow_images: bool = False) -> None:
|
||||
"""Reject unsupported file extensions (400)."""
|
||||
from backend.indexer import SUPPORTED_EXTENSIONS
|
||||
|
||||
ext = file_path.suffix.lower()
|
||||
allowed = SUPPORTED_EXTENSIONS
|
||||
if allow_images:
|
||||
from backend.attachment_indexer import IMAGE_EXTENSIONS
|
||||
allowed = allowed | IMAGE_EXTENSIONS
|
||||
|
||||
if ext not in allowed and file_path.name.lower() not in ("dockerfile", "makefile"):
|
||||
raise ServiceError(
|
||||
f"Unsupported file extension: {ext}",
|
||||
code="unsupported_extension",
|
||||
status=400,
|
||||
details={"extension": ext},
|
||||
)
|
||||
|
||||
|
||||
def _validate_new_name(name: str) -> str:
|
||||
"""Validate a rename target (a plain name, not a path)."""
|
||||
candidate = (name or "").strip()
|
||||
if not candidate or candidate in (".", "..") or "/" in candidate or "\\" in candidate:
|
||||
raise ServiceError(
|
||||
f"Invalid name: {name!r}",
|
||||
code="invalid_arguments",
|
||||
status=400,
|
||||
details={"new_name": name},
|
||||
)
|
||||
return candidate
|
||||
|
||||
|
||||
# ── D1. Creation ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def create_file(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
content: str = "",
|
||||
*,
|
||||
overwrite: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Create a text file in a vault.
|
||||
|
||||
Args:
|
||||
vault_name: Name of the vault.
|
||||
path: Vault-relative path of the new file.
|
||||
content: Initial content (an Excalidraw skeleton is injected for empty
|
||||
``.excalidraw`` files).
|
||||
overwrite: When True, replace an existing file (with a backup) instead
|
||||
of raising ``already_exists``.
|
||||
|
||||
Returns:
|
||||
``{"success", "vault", "path", "size"}``.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) unknown vault, ``read_only`` (403),
|
||||
``unsupported_extension`` (400) or ``already_exists`` (409).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
_validate_extension(file_path)
|
||||
|
||||
if file_path.exists():
|
||||
if not overwrite:
|
||||
raise ServiceError(
|
||||
f"File already exists: {path}",
|
||||
code="already_exists",
|
||||
status=409,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
create_backup(file_path, vault_name, _rel(root, file_path))
|
||||
|
||||
try:
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if file_path.suffix.lower() == ".excalidraw" and not content.strip():
|
||||
content = _EXCALIDRAW_SKELETON
|
||||
file_path.write_text(content, encoding="utf-8")
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot create file", code="permission_denied", status=403) from e
|
||||
|
||||
rel_path = _rel(root, file_path)
|
||||
logger.info(f"File created: {vault_name}/{rel_path}")
|
||||
return {"success": True, "vault": vault_name, "path": rel_path, "size": len(content)}
|
||||
|
||||
|
||||
def create_directory(vault_name: str, path: str) -> dict[str, Any]:
|
||||
"""Create a directory (and its parents) in a vault.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404), ``read_only`` (403) or
|
||||
``already_exists`` (409).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
dir_path = resolve_safe_path(root, path)
|
||||
|
||||
if dir_path.exists():
|
||||
raise ServiceError(
|
||||
f"Directory already exists: {path}",
|
||||
code="already_exists",
|
||||
status=409,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
try:
|
||||
dir_path.mkdir(parents=True, exist_ok=False)
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot create directory", code="permission_denied", status=403) from e
|
||||
|
||||
rel_path = _rel(root, dir_path)
|
||||
logger.info(f"Directory created: {vault_name}/{rel_path}")
|
||||
return {"success": True, "vault": vault_name, "path": rel_path}
|
||||
|
||||
|
||||
# ── D2. Edition / rename / move ────────────────────────────────────────────
|
||||
|
||||
|
||||
def edit_file(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
content: str,
|
||||
*,
|
||||
backup: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""Overwrite an existing file's content (with a backup by default).
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) or ``read_only`` (403).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
rel_path = _rel(root, file_path)
|
||||
if backup:
|
||||
create_backup(file_path, vault_name, rel_path)
|
||||
|
||||
try:
|
||||
file_path.write_text(content, encoding="utf-8")
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot save file", code="permission_denied", status=403) from e
|
||||
|
||||
logger.info(f"File saved: {vault_name}/{rel_path}")
|
||||
return {"success": True, "vault": vault_name, "path": rel_path, "size": len(content)}
|
||||
|
||||
|
||||
def append_to_file(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
content: str,
|
||||
*,
|
||||
backup: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""Append text to an existing file (a newline is inserted if needed).
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) or ``read_only`` (403).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
rel_path = _rel(root, file_path)
|
||||
if backup:
|
||||
create_backup(file_path, vault_name, rel_path)
|
||||
|
||||
try:
|
||||
existing = file_path.read_text(encoding="utf-8", errors="replace")
|
||||
separator = "" if (not existing or existing.endswith("\n")) else "\n"
|
||||
new_content = existing + separator + content
|
||||
file_path.write_text(new_content, encoding="utf-8")
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot append to file", code="permission_denied", status=403) from e
|
||||
|
||||
logger.info(f"File appended: {vault_name}/{rel_path} (+{len(content)} chars)")
|
||||
return {
|
||||
"success": True,
|
||||
"vault": vault_name,
|
||||
"path": rel_path,
|
||||
"appended": len(content),
|
||||
"size": len(new_content),
|
||||
}
|
||||
|
||||
|
||||
def rename_file(vault_name: str, path: str, new_name: str) -> dict[str, Any]:
|
||||
"""Rename a file in place (same parent directory).
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404), ``read_only`` (403),
|
||||
``unsupported_extension`` (400) or ``already_exists`` (409).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
old_path = resolve_safe_path(root, path)
|
||||
|
||||
if not old_path.exists() or not old_path.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
new_path = old_path.parent / _validate_new_name(new_name)
|
||||
new_path = resolve_safe_path(root, _rel(root, new_path))
|
||||
_validate_extension(new_path)
|
||||
|
||||
if new_path.exists():
|
||||
raise ServiceError(
|
||||
f"Destination already exists: {new_name}",
|
||||
code="already_exists",
|
||||
status=409,
|
||||
details={"vault": vault_name, "new_name": new_name},
|
||||
)
|
||||
|
||||
old_rel = _rel(root, old_path)
|
||||
try:
|
||||
old_path.rename(new_path)
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot rename file", code="permission_denied", status=403) from e
|
||||
|
||||
new_rel = _rel(root, new_path)
|
||||
logger.info(f"File renamed: {vault_name}/{old_rel} -> {new_rel}")
|
||||
return {"success": True, "vault": vault_name, "old_path": old_rel, "new_path": new_rel}
|
||||
|
||||
|
||||
def rename_directory(vault_name: str, path: str, new_name: str) -> dict[str, Any]:
|
||||
"""Rename a directory in place (same parent directory).
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404), ``read_only`` (403) or
|
||||
``already_exists`` (409).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
old_path = resolve_safe_path(root, path)
|
||||
|
||||
if not old_path.exists() or not old_path.is_dir():
|
||||
raise ServiceError(
|
||||
f"Directory not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
new_path = old_path.parent / _validate_new_name(new_name)
|
||||
new_path = resolve_safe_path(root, _rel(root, new_path))
|
||||
|
||||
if new_path.exists():
|
||||
raise ServiceError(
|
||||
f"Destination already exists: {new_name}",
|
||||
code="already_exists",
|
||||
status=409,
|
||||
details={"vault": vault_name, "new_name": new_name},
|
||||
)
|
||||
|
||||
old_rel = _rel(root, old_path)
|
||||
try:
|
||||
old_path.rename(new_path)
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot rename directory", code="permission_denied", status=403) from e
|
||||
|
||||
new_rel = _rel(root, new_path)
|
||||
logger.info(f"Directory renamed: {vault_name}/{old_rel} -> {new_rel}")
|
||||
return {"success": True, "vault": vault_name, "old_path": old_rel, "new_path": new_rel}
|
||||
|
||||
|
||||
def move_path(vault_name: str, source_path: str, destination_dir: str = "") -> dict[str, Any]:
|
||||
"""Move a file or directory to another directory within the same vault.
|
||||
|
||||
The item keeps its name; only its parent directory changes.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404), ``read_only`` (403),
|
||||
``unsupported_extension`` (400) or ``already_exists`` (409).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
|
||||
source = resolve_safe_path(root, source_path)
|
||||
if not source.exists():
|
||||
raise ServiceError(
|
||||
f"Source not found: {source_path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": source_path},
|
||||
)
|
||||
|
||||
is_directory = source.is_dir()
|
||||
item_name = source.name
|
||||
dest_clean = (destination_dir or "").strip("/")
|
||||
|
||||
if dest_clean:
|
||||
dest_parent = resolve_safe_path(root, dest_clean)
|
||||
if not dest_parent.exists() or not dest_parent.is_dir():
|
||||
raise ServiceError(
|
||||
f"Destination directory not found: {destination_dir}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": destination_dir},
|
||||
)
|
||||
else:
|
||||
dest_parent = root
|
||||
|
||||
destination = resolve_safe_path(root, _rel(root, dest_parent / item_name))
|
||||
|
||||
if source.resolve() == destination.resolve():
|
||||
rel = _rel(root, source)
|
||||
return {
|
||||
"success": True,
|
||||
"vault": vault_name,
|
||||
"old_path": rel,
|
||||
"new_path": rel,
|
||||
"item_type": "directory" if is_directory else "file",
|
||||
}
|
||||
|
||||
if destination.exists():
|
||||
raise ServiceError(
|
||||
f"A file or directory already exists at the destination: {destination.name}",
|
||||
code="already_exists",
|
||||
status=409,
|
||||
details={"vault": vault_name, "new_path": _rel(root, destination)},
|
||||
)
|
||||
|
||||
if not is_directory:
|
||||
_validate_extension(destination)
|
||||
|
||||
old_rel = _rel(root, source)
|
||||
try:
|
||||
source.rename(destination)
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot move item", code="permission_denied", status=403) from e
|
||||
|
||||
new_rel = _rel(root, destination)
|
||||
item_type = "directory" if is_directory else "file"
|
||||
logger.info(f"Item moved: {vault_name}/{old_rel} -> {new_rel}")
|
||||
return {
|
||||
"success": True,
|
||||
"vault": vault_name,
|
||||
"old_path": old_rel,
|
||||
"new_path": new_rel,
|
||||
"item_type": item_type,
|
||||
}
|
||||
|
||||
|
||||
# ── D3. Find & replace ─────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def replace_in_files(
|
||||
find: str,
|
||||
replacement: str,
|
||||
*,
|
||||
vault: str = "all",
|
||||
case_sensitive: bool = False,
|
||||
whole_word: bool = False,
|
||||
regex: bool = False,
|
||||
include_paths: str | None = None,
|
||||
exclude_paths: str | None = None,
|
||||
replace_all: bool = False,
|
||||
dry_run: bool = True,
|
||||
is_vault_allowed: Callable[[str], bool] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Find and replace text across vault files (dry-run by default).
|
||||
|
||||
A backup is created before every file is rewritten. When
|
||||
``is_vault_allowed`` is provided, files from vaults it rejects are skipped
|
||||
(used by the tool layer to enforce per-vault permissions and the
|
||||
destructive-tools toggle).
|
||||
|
||||
Returns:
|
||||
``{"matches", "total_matches"}`` in dry-run mode (with
|
||||
``"dry_run": True``) or ``{"replaced", "total_replacements"}`` when
|
||||
applied.
|
||||
"""
|
||||
import re as re_mod
|
||||
|
||||
from backend.services.regex_safety import MAX_REGEX_MATCHES, validate_regex
|
||||
from backend.services.search import advanced_search_vaults
|
||||
|
||||
if not find:
|
||||
raise ServiceError("Query is required", code="invalid_arguments", status=400)
|
||||
|
||||
# BUG-025: validate the pattern before it is compiled / applied in bulk.
|
||||
if regex:
|
||||
try:
|
||||
validate_regex(find)
|
||||
except ValueError as e:
|
||||
raise ServiceError(str(e), code="invalid_arguments", status=400) from e
|
||||
|
||||
try:
|
||||
search_results = advanced_search_vaults(
|
||||
find,
|
||||
vault=vault,
|
||||
case_sensitive=case_sensitive,
|
||||
whole_word=whole_word,
|
||||
regex=regex,
|
||||
include_paths=include_paths,
|
||||
exclude_paths=exclude_paths,
|
||||
limit=500,
|
||||
sort="relevance",
|
||||
)
|
||||
except ValueError as e:
|
||||
raise ServiceError(str(e), code="invalid_arguments", status=400) from e
|
||||
|
||||
if not search_results["results"]:
|
||||
return {"matches": [], "total_matches": 0, "dry_run": dry_run}
|
||||
|
||||
flags = 0 if case_sensitive else re_mod.IGNORECASE
|
||||
if regex:
|
||||
pattern = re_mod.compile(find, flags)
|
||||
elif whole_word:
|
||||
pattern = re_mod.compile(rf"\b{re_mod.escape(find)}\b", flags)
|
||||
else:
|
||||
pattern = re_mod.compile(re_mod.escape(find), flags)
|
||||
|
||||
matches: list[dict[str, Any]] = []
|
||||
total = 0
|
||||
|
||||
for result in search_results["results"]:
|
||||
result_vault = result["vault"]
|
||||
if is_vault_allowed is not None and not is_vault_allowed(result_vault):
|
||||
continue
|
||||
try:
|
||||
root = get_vault_root(result_vault)
|
||||
file_path = resolve_safe_path(root, result["path"])
|
||||
except ServiceError:
|
||||
continue
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
continue
|
||||
try:
|
||||
original = file_path.read_text(encoding="utf-8", errors="replace")
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
occurrences = list(pattern.finditer(original))[:MAX_REGEX_MATCHES]
|
||||
if not occurrences:
|
||||
continue
|
||||
|
||||
if dry_run:
|
||||
previews = []
|
||||
for m in occurrences[:3]:
|
||||
start = max(0, m.start() - 40)
|
||||
end = min(len(original), m.end() + 40)
|
||||
previews.append(f"...{original[start:end]}...")
|
||||
matches.append({
|
||||
"vault": result_vault,
|
||||
"path": result["path"],
|
||||
"title": result.get("title", result["path"]),
|
||||
"match_count": len(occurrences),
|
||||
"preview": previews,
|
||||
})
|
||||
total += len(occurrences)
|
||||
continue
|
||||
|
||||
new_content, count = pattern.subn(replacement, original)
|
||||
if count == 0:
|
||||
continue
|
||||
create_backup(file_path, result_vault, result["path"])
|
||||
try:
|
||||
file_path.write_text(new_content, encoding="utf-8")
|
||||
except PermissionError as e:
|
||||
raise ServiceError(
|
||||
f"Permission denied writing {result['path']}",
|
||||
code="permission_denied",
|
||||
status=403,
|
||||
) from e
|
||||
matches.append({
|
||||
"vault": result_vault,
|
||||
"path": result["path"],
|
||||
"title": result.get("title", result["path"]),
|
||||
"replacements": count,
|
||||
"size": len(new_content),
|
||||
})
|
||||
total += count
|
||||
|
||||
if dry_run:
|
||||
return {"matches": matches, "total_matches": total, "dry_run": True}
|
||||
return {"replaced": matches, "total_replacements": total, "dry_run": False}
|
||||
|
||||
|
||||
# ── D4. Deletion / restore ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
def delete_file(vault_name: str, path: str, *, backup: bool = True) -> dict[str, Any]:
|
||||
"""Delete a file (with a backup by default).
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) or ``read_only`` (403).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
|
||||
if not file_path.exists() or not file_path.is_file():
|
||||
raise ServiceError(
|
||||
f"File not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
rel_path = _rel(root, file_path)
|
||||
if backup:
|
||||
create_backup(file_path, vault_name, rel_path)
|
||||
|
||||
try:
|
||||
file_path.unlink()
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot delete file", code="permission_denied", status=403) from e
|
||||
|
||||
logger.info(f"File deleted: {vault_name}/{rel_path}")
|
||||
return {"success": True, "vault": vault_name, "path": rel_path}
|
||||
|
||||
|
||||
def delete_directory(vault_name: str, path: str, *, recursive: bool = True) -> dict[str, Any]:
|
||||
"""Delete a directory (recursively by default).
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404), ``read_only`` (403),
|
||||
``not_empty`` (409) when non-recursive and not empty, or
|
||||
``invalid_arguments`` (400) when targeting the vault root.
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
dir_path = resolve_safe_path(root, path)
|
||||
|
||||
if not dir_path.exists() or not dir_path.is_dir():
|
||||
raise ServiceError(
|
||||
f"Directory not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
if dir_path.resolve() == root.resolve():
|
||||
raise ServiceError(
|
||||
"Refusing to delete the vault root",
|
||||
code="invalid_arguments",
|
||||
status=400,
|
||||
details={"vault": vault_name},
|
||||
)
|
||||
|
||||
file_count = sum(1 for p in dir_path.rglob("*") if p.is_file())
|
||||
|
||||
try:
|
||||
if recursive:
|
||||
shutil.rmtree(dir_path)
|
||||
else:
|
||||
if any(dir_path.iterdir()):
|
||||
raise ServiceError(
|
||||
f"Directory not empty: {path}",
|
||||
code="not_empty",
|
||||
status=409,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
dir_path.rmdir()
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot delete directory", code="permission_denied", status=403) from e
|
||||
|
||||
rel_path = _rel(root, dir_path)
|
||||
logger.info(f"Directory deleted: {vault_name}/{rel_path} ({file_count} files)")
|
||||
return {"success": True, "vault": vault_name, "path": rel_path, "deleted_count": file_count}
|
||||
|
||||
|
||||
def restore_backup(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
version: int,
|
||||
*,
|
||||
backup: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""Restore a file from a backup version.
|
||||
|
||||
The current file is backed up first (when ``backup`` is True) so the
|
||||
operation is reversible.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) when the file or backup is missing,
|
||||
or ``read_only`` (403).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
|
||||
backup_dir = get_backup_dir(vault_name, path)
|
||||
backup_path = backup_dir / f"{Path(path).name}.{version}.bak"
|
||||
if not backup_path.exists():
|
||||
raise ServiceError(
|
||||
f"Backup version {version} not found for {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path, "version": version},
|
||||
)
|
||||
|
||||
# Read the target version *before* backing up the current file: both use a
|
||||
# second-resolution timestamp, so a same-second backup could otherwise
|
||||
# overwrite the version we are about to restore.
|
||||
try:
|
||||
content = backup_path.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
raise ServiceError(
|
||||
f"Failed to read backup {version}: {e}",
|
||||
code="read_error",
|
||||
status=500,
|
||||
details={"vault": vault_name, "path": path, "version": version},
|
||||
) from e
|
||||
|
||||
current_backed_up: int | None = None
|
||||
if backup and file_path.exists() and file_path.is_file():
|
||||
import time as _time
|
||||
|
||||
create_backup(file_path, vault_name, path)
|
||||
current_backed_up = int(_time.time())
|
||||
|
||||
try:
|
||||
file_path.write_text(content, encoding="utf-8")
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot restore file", code="permission_denied", status=403) from e
|
||||
|
||||
logger.info(f"File restored from backup: {vault_name}/{path} <- version {version}")
|
||||
return {
|
||||
"success": True,
|
||||
"vault": vault_name,
|
||||
"path": path,
|
||||
"restored_from": version,
|
||||
"current_backed_up": current_backed_up,
|
||||
}
|
||||
|
||||
|
||||
# ── D3. Batch upload & raw file save ───────────────────────────────────────
|
||||
|
||||
|
||||
def save_raw_file(
|
||||
vault_name: str,
|
||||
path: str,
|
||||
content: bytes,
|
||||
*,
|
||||
overwrite: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""Save a binary or text file to a vault (e.g. from upload / drag-and-drop).
|
||||
|
||||
Creates parent directories automatically and safely validates the path.
|
||||
Supports supported text extensions, images and Excalidraw files.
|
||||
|
||||
Args:
|
||||
vault_name: Name of the vault.
|
||||
path: Vault-relative path.
|
||||
content: Raw bytes to write.
|
||||
overwrite: When True, replace existing files (with backup).
|
||||
|
||||
Returns:
|
||||
Dict with ``success``, ``vault``, ``path``, and ``size``.
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
file_path = resolve_safe_path(root, path)
|
||||
_validate_extension(file_path, allow_images=True)
|
||||
|
||||
rel_path = _rel(root, file_path)
|
||||
|
||||
if file_path.exists():
|
||||
if not overwrite:
|
||||
raise ServiceError(
|
||||
f"File already exists: {rel_path}",
|
||||
code="already_exists",
|
||||
status=409,
|
||||
details={"vault": vault_name, "path": rel_path},
|
||||
)
|
||||
create_backup(file_path, vault_name, rel_path)
|
||||
|
||||
try:
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
file_path.write_bytes(content)
|
||||
except PermissionError as e:
|
||||
raise ServiceError("Permission denied: cannot save file", code="permission_denied", status=403) from e
|
||||
|
||||
logger.info(f"Raw file saved: {vault_name}/{rel_path} ({len(content)} bytes)")
|
||||
return {"success": True, "vault": vault_name, "path": rel_path, "size": len(content)}
|
||||
|
||||
|
||||
def batch_upload_files(
|
||||
vault_name: str,
|
||||
target_dir: str,
|
||||
files: list[dict[str, Any]],
|
||||
*,
|
||||
overwrite: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""Process a batch of uploaded files and directories into a vault.
|
||||
|
||||
Args:
|
||||
vault_name: Name of the target vault.
|
||||
target_dir: Base directory inside the vault (empty string for root).
|
||||
files: List of dicts, each with:
|
||||
- ``path``: relative path within the batch (e.g. ``"sub/doc.md"`` or ``"note.md"``).
|
||||
- ``content``: bytes content (or base64 decoded).
|
||||
- ``is_dir``: optional boolean for empty directories.
|
||||
overwrite: Whether to overwrite existing files.
|
||||
|
||||
Returns:
|
||||
Dict with ``uploaded`` (list of paths), ``created_dirs`` (list of paths),
|
||||
and ``errors`` (list of error dicts).
|
||||
"""
|
||||
root = get_vault_root(vault_name)
|
||||
_ensure_writable(root)
|
||||
|
||||
clean_target = (target_dir or "").strip().strip("/\\")
|
||||
uploaded: list[str] = []
|
||||
created_dirs: list[str] = []
|
||||
errors: list[dict[str, Any]] = []
|
||||
|
||||
for item in files:
|
||||
rel_subpath = (item.get("path") or "").strip().replace("\\", "/").lstrip("/")
|
||||
if not rel_subpath:
|
||||
continue
|
||||
|
||||
full_rel_path = f"{clean_target}/{rel_subpath}" if clean_target else rel_subpath
|
||||
is_dir = item.get("is_dir", False)
|
||||
|
||||
if is_dir:
|
||||
try:
|
||||
dir_path = resolve_safe_path(root, full_rel_path)
|
||||
dir_path.mkdir(parents=True, exist_ok=True)
|
||||
created_dirs.append(_rel(root, dir_path))
|
||||
except Exception as e:
|
||||
errors.append({"path": full_rel_path, "error": str(e)})
|
||||
continue
|
||||
|
||||
raw_bytes = item.get("content", b"")
|
||||
if isinstance(raw_bytes, str):
|
||||
raw_bytes = raw_bytes.encode("utf-8")
|
||||
|
||||
try:
|
||||
res = save_raw_file(vault_name, full_rel_path, raw_bytes, overwrite=overwrite)
|
||||
uploaded.append(res["path"])
|
||||
except Exception as e:
|
||||
errors.append({"path": full_rel_path, "error": str(e)})
|
||||
|
||||
return {
|
||||
"success": len(errors) == 0,
|
||||
"vault": vault_name,
|
||||
"target_dir": clean_target,
|
||||
"uploaded": uploaded,
|
||||
"created_dirs": created_dirs,
|
||||
"errors": errors,
|
||||
"total_files": len(uploaded),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Network helpers shared by the auth middleware and rate limiter."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from fastapi import Request
|
||||
|
||||
__all__ = ["get_client_ip", "is_trusted_proxy"]
|
||||
|
||||
|
||||
def is_trusted_proxy() -> bool:
|
||||
"""Whether ``X-Forwarded-For`` should be trusted (reverse proxy in front)."""
|
||||
return os.environ.get("OBSIGATE_TRUST_PROXY", "false").lower() == "true"
|
||||
|
||||
|
||||
def get_client_ip(request: Request) -> str:
|
||||
"""Return the best-known client IP for *request*.
|
||||
|
||||
When ``OBSIGATE_TRUST_PROXY=true`` the left-most ``X-Forwarded-For`` entry
|
||||
is used (the original client behind the proxy). Otherwise the socket peer
|
||||
address is returned. BUG-030: this value feeds the audit log so attacks
|
||||
remain traceable.
|
||||
"""
|
||||
if is_trusted_proxy():
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
if forwarded:
|
||||
first = forwarded.split(",")[0].strip()
|
||||
if first:
|
||||
return first
|
||||
real_ip = request.headers.get("x-real-ip")
|
||||
if real_ip:
|
||||
return real_ip.strip()
|
||||
return request.client.host if request.client else "unknown"
|
||||
@@ -0,0 +1,62 @@
|
||||
"""Vault path resolution shared by the REST routes and the AI tool layer.
|
||||
|
||||
This is the single implementation of the anti path-traversal check. Routes map
|
||||
:class:`ServiceError` to ``HTTPException`` and tools map it to
|
||||
:class:`backend.tools.context.ToolError`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from backend.services.errors import ServiceError
|
||||
|
||||
logger = logging.getLogger("obsigate.services.paths")
|
||||
|
||||
|
||||
def _is_within(resolved: Path, root: Path) -> bool:
|
||||
"""Return True when *resolved* is *root* or lives below it.
|
||||
|
||||
The comparison is segment-aware so that a sibling directory whose name
|
||||
merely shares a prefix (``vault`` vs ``vault-evil``) is rejected. A
|
||||
case-insensitive fallback preserves the Windows / Docker behaviour where
|
||||
the resolved casing can differ from the configured root.
|
||||
"""
|
||||
try:
|
||||
resolved.relative_to(root)
|
||||
return True
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
resolved_parts = tuple(part.lower() for part in resolved.parts)
|
||||
root_parts = tuple(part.lower() for part in root.parts)
|
||||
except Exception:
|
||||
return False
|
||||
return resolved_parts[: len(root_parts)] == root_parts
|
||||
|
||||
|
||||
def resolve_safe_path(vault_root: Path, relative_path: str | None) -> Path:
|
||||
"""Resolve a vault-relative path, rejecting traversal outside the vault.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``path_error`` (500) when the path cannot be resolved,
|
||||
``path_outside_vault`` (403) when it escapes the vault root.
|
||||
"""
|
||||
full_path = vault_root / (relative_path or "")
|
||||
try:
|
||||
resolved = full_path.resolve(strict=False)
|
||||
root = vault_root.resolve(strict=False)
|
||||
except Exception as e:
|
||||
logger.error(f"Path resolution error - vault_root: {vault_root}, relative_path: {relative_path}, error: {e}")
|
||||
raise ServiceError(f"Path resolution error: {e!s}", code="path_error", status=500) from e
|
||||
|
||||
if not _is_within(resolved, root):
|
||||
logger.warning(f"Path outside vault - vault: {root}, requested: {relative_path}, resolved: {resolved}")
|
||||
raise ServiceError(
|
||||
"Access denied: path outside vault",
|
||||
code="path_outside_vault",
|
||||
status=403,
|
||||
details={"path": relative_path},
|
||||
)
|
||||
return resolved
|
||||
@@ -0,0 +1,138 @@
|
||||
"""Recent-files services shared by the REST route and the AI tool layer.
|
||||
|
||||
Provides ``list_recent`` (last opened files, falling back to last modified)
|
||||
and ``humanize_mtime``. The route ``/api/recent`` and the tool ``list_recent``
|
||||
both delegate here. Permission filtering is applied against the caller's
|
||||
vault list.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from backend.history import get_recent_opened, is_bookmarked
|
||||
from backend.indexer import find_file_in_index, index
|
||||
|
||||
|
||||
def humanize_mtime(mtime: float) -> str:
|
||||
"""Return a short, human-friendly French rendering of a timestamp."""
|
||||
delta = time.time() - mtime
|
||||
if delta < 60:
|
||||
return "à l'instant"
|
||||
if delta < 3600:
|
||||
return f"il y a {int(delta / 60)} min"
|
||||
if delta < 86400:
|
||||
return f"il y a {int(delta / 3600)} h"
|
||||
if delta < 604800:
|
||||
return f"il y a {int(delta / 86400)} j"
|
||||
return datetime.fromtimestamp(mtime).strftime("%d %b %Y")
|
||||
|
||||
|
||||
def _can_access(vault: str, user_vaults: list[str]) -> bool:
|
||||
return "*" in user_vaults or vault in user_vaults
|
||||
|
||||
|
||||
def list_recent(
|
||||
username: str | None,
|
||||
user_vaults: list[str] | None = None,
|
||||
*,
|
||||
vault: str | None = None,
|
||||
limit: int = 20,
|
||||
mode: str = "opened",
|
||||
) -> dict[str, Any]:
|
||||
"""Return the caller's recent files.
|
||||
|
||||
Args:
|
||||
username: Caller username (needed for the "opened" history mode).
|
||||
user_vaults: Vaults the caller may access (``["*"]`` = all).
|
||||
vault: Optional single-vault filter.
|
||||
limit: Maximum number of files to return.
|
||||
mode: ``"opened"`` to use the open history, anything else for the
|
||||
last-modified fallback.
|
||||
|
||||
Returns:
|
||||
Dict with ``files``, ``total``, ``limit`` and ``mode``.
|
||||
"""
|
||||
user_vaults = user_vaults or []
|
||||
|
||||
if mode == "opened" and username:
|
||||
history = get_recent_opened(username, vault_filter=vault, limit=limit)
|
||||
files_resp: list[dict[str, Any]] = []
|
||||
for item in history:
|
||||
v_name = item["vault"]
|
||||
if not _can_access(v_name, user_vaults):
|
||||
continue
|
||||
|
||||
f_idx = find_file_in_index(item["path"], v_name)
|
||||
if f_idx:
|
||||
files_resp.append({
|
||||
"path": f_idx["path"],
|
||||
"title": f_idx.get("title") or item["path"].split("/")[-1],
|
||||
"vault": v_name,
|
||||
"mtime": item["opened_at"],
|
||||
"mtime_human": humanize_mtime(item["opened_at"]),
|
||||
"size_bytes": f_idx.get("size", 0),
|
||||
"tags": [f"#{t}" for t in f_idx.get("tags", [])][:5],
|
||||
"preview": f_idx.get("content_preview", "")[:120],
|
||||
"bookmarked": is_bookmarked(username, v_name, f_idx["path"]),
|
||||
})
|
||||
else:
|
||||
files_resp.append({
|
||||
"path": item["path"],
|
||||
"title": item.get("title") or item["path"].split("/")[-1],
|
||||
"vault": v_name,
|
||||
"mtime": item["opened_at"],
|
||||
"mtime_human": humanize_mtime(item["opened_at"]),
|
||||
"tags": [],
|
||||
"preview": "",
|
||||
"bookmarked": is_bookmarked(username, v_name, item["path"]),
|
||||
})
|
||||
return {
|
||||
"files": files_resp,
|
||||
"total": len(files_resp),
|
||||
"limit": limit,
|
||||
"mode": "opened",
|
||||
}
|
||||
|
||||
all_files: list[tuple[str, dict[str, Any]]] = []
|
||||
for v_name, v_data in index.items():
|
||||
if vault and v_name != vault:
|
||||
continue
|
||||
if not _can_access(v_name, user_vaults):
|
||||
continue
|
||||
for f in v_data.get("files", []):
|
||||
all_files.append((v_name, f))
|
||||
|
||||
all_files.sort(key=lambda x: x[1].get("modified", ""), reverse=True)
|
||||
recent = all_files[:limit]
|
||||
|
||||
files_resp = []
|
||||
for v_name, f in recent:
|
||||
iso_modified = f.get("modified", "")
|
||||
try:
|
||||
mtime_dt = datetime.fromisoformat(iso_modified.replace("Z", "+00:00"))
|
||||
mtime_val = mtime_dt.timestamp()
|
||||
except Exception:
|
||||
mtime_val = time.time()
|
||||
|
||||
files_resp.append({
|
||||
"path": f["path"],
|
||||
"title": f["title"],
|
||||
"vault": v_name,
|
||||
"mtime": mtime_val,
|
||||
"mtime_human": humanize_mtime(mtime_val),
|
||||
"mtime_iso": iso_modified,
|
||||
"size_bytes": f.get("size", 0),
|
||||
"tags": [f"#{t}" for t in f.get("tags", [])][:5],
|
||||
"preview": f.get("content_preview", "")[:120],
|
||||
"bookmarked": is_bookmarked(username, v_name, f["path"]) if username else False,
|
||||
})
|
||||
|
||||
return {
|
||||
"files": files_resp,
|
||||
"total": len(all_files),
|
||||
"limit": limit,
|
||||
"mode": "modified",
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Regex safety helpers (BUG-025).
|
||||
|
||||
User-supplied regular expressions are applied to large amounts of indexed
|
||||
content. Python's :mod:`re` has no timeout, so a malicious pattern such as
|
||||
``(a+)+$`` can pin a CPU for a long time (ReDoS). Without adding a native
|
||||
dependency we mitigate by:
|
||||
|
||||
* bounding the pattern length,
|
||||
* rejecting nested-quantifier constructs (the classic catastrophic form),
|
||||
* capping the amount of text a single regex pass may scan,
|
||||
* capping the number of matches collected.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
__all__ = [
|
||||
"MAX_PATTERN_LENGTH",
|
||||
"MAX_REGEX_CONTENT",
|
||||
"MAX_REGEX_MATCHES",
|
||||
"truncate_for_regex",
|
||||
"validate_regex",
|
||||
]
|
||||
|
||||
MAX_PATTERN_LENGTH = 500
|
||||
MAX_REGEX_CONTENT = 200_000
|
||||
MAX_REGEX_MATCHES = 1000
|
||||
|
||||
# A quantified group whose body already contains a quantifier, immediately
|
||||
# followed by another quantifier: ``(a+)+``, ``(.*)*``, ``(a+){2,}``, ...
|
||||
_NESTED_QUANTIFIER_RE = re.compile(r"\([^()]*[+*][^()]*\)\s*(?:[+*?]|\{)")
|
||||
# Backreferences combined with quantifiers are a common ReDoS vector too.
|
||||
_BACKREF_QUANTIFIER_RE = re.compile(r"\\[1-9][0-9]*\s*(?:[+*]|\{)")
|
||||
|
||||
|
||||
def validate_regex(pattern: str) -> str:
|
||||
"""Validate a user-supplied regex against the safety policy.
|
||||
|
||||
Args:
|
||||
pattern: Raw regex pattern.
|
||||
|
||||
Returns:
|
||||
The pattern unchanged when acceptable.
|
||||
|
||||
Raises:
|
||||
ValueError: When the pattern is empty, too long, or uses a construct
|
||||
known to cause catastrophic backtracking.
|
||||
"""
|
||||
if not pattern:
|
||||
raise ValueError("Expression régulière vide")
|
||||
if len(pattern) > MAX_PATTERN_LENGTH:
|
||||
raise ValueError(f"Expression régulière trop longue (max {MAX_PATTERN_LENGTH})")
|
||||
if _NESTED_QUANTIFIER_RE.search(pattern) or _BACKREF_QUANTIFIER_RE.search(pattern):
|
||||
raise ValueError("Expression régulière refusée (quantificateurs imbriqués)")
|
||||
try:
|
||||
re.compile(pattern)
|
||||
except re.error as e:
|
||||
raise ValueError(f"Expression régulière invalide : {e}") from e
|
||||
return pattern
|
||||
|
||||
|
||||
def truncate_for_regex(content: str, limit: int = MAX_REGEX_CONTENT) -> str:
|
||||
"""Return at most *limit* characters to bound a single regex pass."""
|
||||
if content and len(content) > limit:
|
||||
return content[:limit]
|
||||
return content
|
||||
@@ -0,0 +1,286 @@
|
||||
"""Whitelist HTML sanitizer used for untrusted markdown / AI output.
|
||||
|
||||
The markdown renderer runs with ``escape=False`` so raw HTML authored inside a
|
||||
vault (or returned by a model) reaches the browser. This module scrubs the
|
||||
rendered HTML against a strict whitelist of tags and attributes, drops
|
||||
dangerous URL schemes and strips every event handler / ``style`` attribute.
|
||||
|
||||
Implemented with the standard library only (no third-party dependency) so the
|
||||
runtime footprint stays unchanged. It is *not* a full HTML5 parser: it is a
|
||||
conservative, allow-list based filter intended for already well-formed output
|
||||
produced by mistune and the image/wikilink pre-processors.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import html as _html
|
||||
from html.parser import HTMLParser
|
||||
|
||||
__all__ = ["is_safe_url", "sanitize_html"]
|
||||
|
||||
# Tags whose *content* is discarded entirely (never rendered as text).
|
||||
_DROP_CONTENT_TAGS = frozenset({
|
||||
"script", "style", "iframe", "object", "embed", "template", "noscript",
|
||||
"svg", "math", "applet", "form", "button", "select", "textarea", "option",
|
||||
"frame", "frameset", "base", "link", "meta", "title", "head",
|
||||
})
|
||||
|
||||
# Tags kept in the output (text content preserved for unknown tags).
|
||||
_ALLOWED_TAGS = frozenset({
|
||||
"a", "abbr", "b", "blockquote", "br", "caption", "code", "col", "colgroup",
|
||||
"dd", "del", "details", "div", "dl", "dt", "em", "figcaption", "figure",
|
||||
"h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "input", "kbd", "li",
|
||||
"mark", "ol", "p", "pre", "q", "s", "section", "small", "span", "strong",
|
||||
"sub", "summary", "sup", "table", "tbody", "td", "tfoot", "th", "thead",
|
||||
"time", "tr", "u", "ul", "video", "audio", "source", "track",
|
||||
})
|
||||
|
||||
# Attributes allowed on any element.
|
||||
_GLOBAL_ATTRS = frozenset({"class", "id", "title", "dir", "lang", "role"})
|
||||
|
||||
# Per-tag attribute whitelist (in addition to globals and ``data-*``).
|
||||
_TAG_ATTRS: dict[str, frozenset[str]] = {
|
||||
"a": frozenset({"href", "target", "rel", "name", "download"}),
|
||||
"img": frozenset({"src", "alt", "width", "height", "loading"}),
|
||||
"input": frozenset({"type", "checked", "disabled", "value"}),
|
||||
"ol": frozenset({"start", "type", "reversed"}),
|
||||
"ul": frozenset({"type"}),
|
||||
"li": frozenset({"value"}),
|
||||
"td": frozenset({"colspan", "rowspan", "align", "valign"}),
|
||||
"th": frozenset({"colspan", "rowspan", "align", "valign", "scope"}),
|
||||
"col": frozenset({"span", "width"}),
|
||||
"colgroup": frozenset({"span"}),
|
||||
"video": frozenset({"src", "controls", "width", "height", "loop", "muted",
|
||||
"poster", "preload", "playsinline"}),
|
||||
"audio": frozenset({"src", "controls", "loop", "muted", "preload"}),
|
||||
"source": frozenset({"src", "type", "srcset", "media"}),
|
||||
"track": frozenset({"src", "kind", "srclang", "label", "default"}),
|
||||
"details": frozenset({"open"}),
|
||||
"time": frozenset({"datetime"}),
|
||||
"blockquote": frozenset({"cite"}),
|
||||
"q": frozenset({"cite"}),
|
||||
}
|
||||
|
||||
# URL-bearing attributes per tag, and whether ``data:`` URIs are acceptable.
|
||||
_URL_ATTRS: dict[str, frozenset[str]] = {
|
||||
"a": frozenset({"href"}),
|
||||
"img": frozenset({"src"}),
|
||||
"video": frozenset({"src", "poster"}),
|
||||
"audio": frozenset({"src"}),
|
||||
"source": frozenset({"src", "srcset"}),
|
||||
"track": frozenset({"src"}),
|
||||
"blockquote": frozenset({"cite"}),
|
||||
"q": frozenset({"cite"}),
|
||||
}
|
||||
|
||||
_SAFE_SCHEMES = frozenset({"http", "https", "mailto", "tel", "ftp"})
|
||||
|
||||
# Characters that browsers ignore inside a scheme (tab/newline/CR) and that
|
||||
# could otherwise smuggle ``java\tscript:`` past a naive check.
|
||||
_URL_STRIP_CHARS = "\t\n\r\x00"
|
||||
|
||||
|
||||
def is_safe_url(value: str, *, allow_data: bool = False, tag: str = "") -> bool:
|
||||
"""Return True when *value* is a URL with an allowed scheme.
|
||||
|
||||
Relative URLs (``/foo``, ``./foo``, ``#anchor``) are allowed. Dangerous
|
||||
schemes such as ``javascript:`` and ``vbscript:`` are always rejected.
|
||||
``data:`` URIs are only allowed for image/video/audio sources.
|
||||
"""
|
||||
if value is None:
|
||||
return False
|
||||
# Decode entities and strip whitespace/control chars before inspecting.
|
||||
candidate = _html.unescape(str(value)).strip()
|
||||
for ch in _URL_STRIP_CHARS:
|
||||
candidate = candidate.replace(ch, "")
|
||||
if not candidate:
|
||||
return False
|
||||
|
||||
# Detect a scheme: ``scheme:`` where scheme is [a-zA-Z][a-zA-Z0-9+.-]*
|
||||
lowered = candidate.lower()
|
||||
if lowered.startswith("data:"):
|
||||
if not allow_data:
|
||||
return False
|
||||
# Only media data URIs are permitted.
|
||||
if tag in ("img",):
|
||||
return lowered.startswith("data:image/")
|
||||
if tag in ("video", "audio", "source", "track"):
|
||||
return (
|
||||
lowered.startswith("data:image/")
|
||||
or lowered.startswith("data:video/")
|
||||
or lowered.startswith("data:audio/")
|
||||
)
|
||||
return False
|
||||
|
||||
if lowered.startswith("blob:"):
|
||||
return tag in ("img", "video", "audio", "source")
|
||||
|
||||
# No scheme at all (relative / fragment / protocol-relative) → safe.
|
||||
colon = candidate.find(":")
|
||||
slash = candidate.find("/")
|
||||
if colon == -1 or (slash != -1 and slash < colon):
|
||||
return True
|
||||
# ``//host`` protocol-relative has no scheme.
|
||||
if candidate.startswith("//"):
|
||||
return True
|
||||
|
||||
scheme = lowered[:colon]
|
||||
if not scheme or not scheme[0].isalpha():
|
||||
return True # not a real scheme, treat as relative
|
||||
return scheme in _SAFE_SCHEMES
|
||||
|
||||
|
||||
class _Sanitizer(HTMLParser):
|
||||
"""Rebuild HTML while dropping anything not explicitly allowed."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(convert_charrefs=True)
|
||||
self._out: list[str] = []
|
||||
# Stack of tag names currently open (only allowed tags).
|
||||
self._open: list[str] = []
|
||||
# Stack tracking dropped-content depth: each entry is the tag name.
|
||||
self._suppress: list[str] = []
|
||||
|
||||
# -- helpers ----------------------------------------------------------
|
||||
def _filter_attrs(self, tag: str, attrs: list[tuple[str, str | None]]) -> str:
|
||||
allowed_extra = _TAG_ATTRS.get(tag, frozenset())
|
||||
url_attrs = _URL_ATTRS.get(tag, frozenset())
|
||||
parts: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for name, value in attrs:
|
||||
if value is None:
|
||||
value = ""
|
||||
lname = name.lower()
|
||||
if lname in seen:
|
||||
continue
|
||||
seen.add(lname)
|
||||
# Event handlers and style are never allowed.
|
||||
if lname.startswith("on") or lname in ("style", "srcdoc", "formaction", "xlink:href"):
|
||||
continue
|
||||
if lname.startswith("data-") or lname.startswith("aria-"):
|
||||
pass
|
||||
elif lname not in _GLOBAL_ATTRS and lname not in allowed_extra:
|
||||
continue
|
||||
|
||||
if lname in url_attrs:
|
||||
allow_data = tag in ("img", "video", "audio", "source", "track")
|
||||
# ``srcset`` may contain multiple comma-separated candidates.
|
||||
if lname == "srcset":
|
||||
if not _safe_srcset(value, tag):
|
||||
continue
|
||||
elif not is_safe_url(value, allow_data=allow_data, tag=tag):
|
||||
continue
|
||||
parts.append(f' {lname}="{_html.escape(value, quote=True)}"')
|
||||
return "".join(parts)
|
||||
|
||||
def _emit_start(self, tag: str, attrs, self_closing: bool) -> None:
|
||||
attrs_html = self._filter_attrs(tag, attrs)
|
||||
if self_closing or tag in ("br", "hr", "img", "input", "col", "source", "track"):
|
||||
self._out.append(f"<{tag}{attrs_html} />")
|
||||
else:
|
||||
self._out.append(f"<{tag}{attrs_html}>")
|
||||
self._open.append(tag)
|
||||
|
||||
# -- HTMLParser callbacks --------------------------------------------
|
||||
def handle_starttag(self, tag: str, attrs) -> None:
|
||||
tag = tag.lower()
|
||||
if tag in _DROP_CONTENT_TAGS:
|
||||
self._suppress.append(tag)
|
||||
return
|
||||
if self._suppress:
|
||||
return
|
||||
if tag not in _ALLOWED_TAGS:
|
||||
return # drop the tag, keep its text content
|
||||
self._emit_start(tag, attrs, self_closing=False)
|
||||
|
||||
def handle_startendtag(self, tag: str, attrs) -> None:
|
||||
tag = tag.lower()
|
||||
if tag in _DROP_CONTENT_TAGS or self._suppress:
|
||||
return
|
||||
if tag not in _ALLOWED_TAGS:
|
||||
return
|
||||
self._emit_start(tag, attrs, self_closing=True)
|
||||
|
||||
def handle_endtag(self, tag: str) -> None:
|
||||
tag = tag.lower()
|
||||
if tag in _DROP_CONTENT_TAGS:
|
||||
# Close the innermost matching suppress marker.
|
||||
for i in range(len(self._suppress) - 1, -1, -1):
|
||||
if self._suppress[i] == tag:
|
||||
del self._suppress[i:]
|
||||
break
|
||||
return
|
||||
if self._suppress:
|
||||
return
|
||||
if tag not in _ALLOWED_TAGS:
|
||||
return
|
||||
# Only close if currently open (tolerate malformed nesting).
|
||||
if tag in self._open:
|
||||
while self._open:
|
||||
top = self._open.pop()
|
||||
self._out.append(f"</{top}>")
|
||||
if top == tag:
|
||||
break
|
||||
|
||||
def handle_data(self, data: str) -> None:
|
||||
if self._suppress:
|
||||
return
|
||||
self._out.append(_html.escape(data, quote=False))
|
||||
|
||||
def handle_comment(self, data: str) -> None:
|
||||
return # comments are dropped
|
||||
|
||||
def handle_decl(self, decl: str) -> None:
|
||||
return
|
||||
|
||||
def handle_pi(self, data: str) -> None:
|
||||
return
|
||||
|
||||
def handle_entityref(self, name: str) -> None:
|
||||
if self._suppress:
|
||||
return
|
||||
self._out.append(f"&{name};")
|
||||
|
||||
def handle_charref(self, name: str) -> None:
|
||||
if self._suppress:
|
||||
return
|
||||
self._out.append(f"&#{name};")
|
||||
|
||||
def get_html(self) -> str:
|
||||
# Close any tags left open by malformed input.
|
||||
while self._open:
|
||||
self._out.append(f"</{self._open.pop()}>")
|
||||
return "".join(self._out)
|
||||
|
||||
|
||||
def _safe_srcset(value: str, tag: str) -> bool:
|
||||
"""Validate every candidate in a ``srcset`` attribute."""
|
||||
for candidate in value.split(","):
|
||||
candidate = candidate.strip()
|
||||
if not candidate:
|
||||
continue
|
||||
url = candidate.split()[0] if candidate.split() else candidate
|
||||
if not is_safe_url(url, allow_data=(tag == "img"), tag=tag):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def sanitize_html(html: str) -> str:
|
||||
"""Return *html* with only whitelisted tags/attributes/schemes preserved.
|
||||
|
||||
Args:
|
||||
html: Untrusted HTML (typically mistune output with ``escape=False``).
|
||||
|
||||
Returns:
|
||||
Sanitized HTML string.
|
||||
"""
|
||||
if not html:
|
||||
return ""
|
||||
parser = _Sanitizer()
|
||||
try:
|
||||
parser.feed(html)
|
||||
parser.close()
|
||||
except Exception:
|
||||
# Never let sanitization crash a request; fail closed to plain text.
|
||||
return _html.escape(html)
|
||||
return parser.get_html()
|
||||
@@ -0,0 +1,144 @@
|
||||
"""Search services shared by REST routes and the AI tool layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def search_vaults(
|
||||
q: str,
|
||||
vault: str = "all",
|
||||
tag: str | None = None,
|
||||
limit: int = 50,
|
||||
offset: int = 0,
|
||||
) -> dict[str, Any]:
|
||||
"""Full-text search with pagination, returned as the API response payload.
|
||||
|
||||
No permission filtering is applied here: callers that need it (the tool
|
||||
layer) filter the ``results`` list themselves.
|
||||
"""
|
||||
from backend.search import search
|
||||
|
||||
all_results = search(q, vault_filter=vault, tag_filter=tag)
|
||||
total = len(all_results)
|
||||
page = all_results[offset: offset + limit]
|
||||
return {
|
||||
"query": q,
|
||||
"vault_filter": vault,
|
||||
"tag_filter": tag,
|
||||
"count": len(page),
|
||||
"total": total,
|
||||
"offset": offset,
|
||||
"limit": limit,
|
||||
"results": page,
|
||||
}
|
||||
|
||||
|
||||
def list_tags(vault: str | None = None) -> dict[str, int]:
|
||||
"""Return tag → count, optionally restricted to a single vault."""
|
||||
from backend.search import get_all_tags
|
||||
|
||||
return get_all_tags(vault_filter=vault)
|
||||
|
||||
|
||||
def advanced_search_vaults(
|
||||
query: str = "",
|
||||
vault: str = "all",
|
||||
tag: str | None = None,
|
||||
limit: int = 50,
|
||||
offset: int = 0,
|
||||
sort: str = "relevance",
|
||||
case_sensitive: bool = False,
|
||||
whole_word: bool = False,
|
||||
regex: bool = False,
|
||||
include_paths: str | None = None,
|
||||
exclude_paths: str | None = None,
|
||||
created: str | None = None,
|
||||
modified: str | None = None,
|
||||
size: str | None = None,
|
||||
semantic: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Advanced full-text search (TF-IDF, facets, operators).
|
||||
|
||||
When ``semantic`` is True, the TF-IDF ranking is fused with the embedding
|
||||
(semantic) ranking via RRF. No permission filtering is applied: callers
|
||||
that need it (the tool layer) filter the ``results`` list themselves.
|
||||
"""
|
||||
from backend.search import advanced_search
|
||||
|
||||
return advanced_search(
|
||||
query,
|
||||
vault_filter=vault,
|
||||
tag_filter=tag,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
sort_by=sort,
|
||||
case_sensitive=case_sensitive,
|
||||
whole_word=whole_word,
|
||||
regex=regex,
|
||||
include_paths=include_paths,
|
||||
exclude_paths=exclude_paths,
|
||||
created=created,
|
||||
modified=modified,
|
||||
size=size,
|
||||
semantic=semantic,
|
||||
)
|
||||
|
||||
|
||||
def search_paths(q: str, vault: str = "all") -> dict[str, Any]:
|
||||
"""Search files and directories by path substring using the path index.
|
||||
|
||||
No permission filtering is applied: callers that need it (the tool layer)
|
||||
filter the ``results`` list themselves.
|
||||
"""
|
||||
from backend.indexer import path_index
|
||||
|
||||
if not q:
|
||||
return {"query": q, "vault_filter": vault, "results": []}
|
||||
|
||||
query_lower = q.lower()
|
||||
results: list[dict[str, Any]] = []
|
||||
|
||||
vaults_to_search = [vault] if vault != "all" else list(path_index.keys())
|
||||
|
||||
for vault_name in vaults_to_search:
|
||||
for entry in path_index.get(vault_name, []):
|
||||
if query_lower in entry["name"].lower() or query_lower in entry["path"].lower():
|
||||
results.append({
|
||||
"vault": vault_name,
|
||||
"path": entry["path"],
|
||||
"name": entry["name"],
|
||||
"type": entry["type"],
|
||||
"matched_path": entry["path"],
|
||||
})
|
||||
|
||||
return {"query": q, "vault_filter": vault, "results": results}
|
||||
|
||||
|
||||
def list_paths(vault: str, limit: int = 5000) -> dict[str, Any]:
|
||||
"""Return a flat, capped list of every indexed path in a vault.
|
||||
|
||||
Backs the AI assistant ``@`` mention menu: fetching the whole path index
|
||||
once lets the client filter files/directories instantly instead of issuing
|
||||
a request per keystroke.
|
||||
|
||||
Args:
|
||||
vault: Vault name.
|
||||
limit: Maximum number of entries returned.
|
||||
|
||||
Returns:
|
||||
``{"vault", "count", "results": [{vault, path, name, type}]}``.
|
||||
"""
|
||||
from backend.indexer import path_index
|
||||
|
||||
entries = path_index.get(vault, [])
|
||||
results = [
|
||||
{
|
||||
"vault": vault,
|
||||
"path": entry["path"],
|
||||
"name": entry["name"],
|
||||
"type": entry["type"],
|
||||
}
|
||||
for entry in entries[: max(0, limit)]
|
||||
]
|
||||
return {"vault": vault, "count": len(results), "results": results}
|
||||
@@ -0,0 +1,214 @@
|
||||
"""Vault listing and directory browsing services.
|
||||
|
||||
Single source of truth consumed by both the REST routes (``/api/vaults``,
|
||||
``/api/browse/{vault}``) and the AI tool layer (``list_vaults``,
|
||||
``list_directory``).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.services.paths import resolve_safe_path
|
||||
|
||||
|
||||
def list_accessible_vaults(user: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
"""Return the vaults *user* may access, with summary metadata."""
|
||||
from backend.auth.middleware import check_vault_access
|
||||
from backend.indexer import index
|
||||
|
||||
result: list[dict[str, Any]] = []
|
||||
for name, data in index.items():
|
||||
if not check_vault_access(name, user):
|
||||
continue
|
||||
result.append({
|
||||
"name": name,
|
||||
"file_count": len(data.get("files", [])),
|
||||
"tag_count": len(data.get("tags", {})),
|
||||
"type": data.get("config", {}).get("type", "VAULT"),
|
||||
})
|
||||
return result
|
||||
|
||||
|
||||
def get_vault_root(vault_name: str) -> Path:
|
||||
"""Return the filesystem root of *vault_name* or raise ``not_found``."""
|
||||
from backend.indexer import get_vault_data
|
||||
|
||||
data = get_vault_data(vault_name)
|
||||
if not data:
|
||||
raise ServiceError(
|
||||
f"Vault '{vault_name}' not found",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name},
|
||||
)
|
||||
return Path(data["path"])
|
||||
|
||||
|
||||
def browse_directory(vault_name: str, path: str = "") -> dict[str, Any]:
|
||||
"""Return the direct children of a vault directory (directories first)."""
|
||||
from backend.indexer import SUPPORTED_EXTENSIONS
|
||||
from backend.vault_settings import get_vault_setting
|
||||
|
||||
root = get_vault_root(vault_name)
|
||||
target = resolve_safe_path(root, path) if path else root.resolve()
|
||||
|
||||
if not target.exists():
|
||||
raise ServiceError(
|
||||
f"Path not found: {path}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": path},
|
||||
)
|
||||
|
||||
hide_hidden = (get_vault_setting(vault_name) or {}).get("hideHiddenFiles", False)
|
||||
|
||||
items: list[dict[str, Any]] = []
|
||||
try:
|
||||
for entry in sorted(target.iterdir(), key=lambda e: (not e.is_dir(), e.name.lower())):
|
||||
if hide_hidden and entry.name.startswith("."):
|
||||
continue
|
||||
rel = str(entry.relative_to(root)).replace("\\", "/")
|
||||
if entry.is_dir():
|
||||
# Count only direct children (files and subdirs) for performance.
|
||||
try:
|
||||
file_count = sum(
|
||||
1 for child in entry.iterdir()
|
||||
if (not hide_hidden or not child.name.startswith("."))
|
||||
and (child.is_file() and (child.suffix.lower() in SUPPORTED_EXTENSIONS or child.name.lower() in ("dockerfile", "makefile"))
|
||||
or child.is_dir())
|
||||
)
|
||||
except PermissionError:
|
||||
file_count = 0
|
||||
items.append({
|
||||
"name": entry.name,
|
||||
"path": rel,
|
||||
"type": "directory",
|
||||
"children_count": file_count,
|
||||
})
|
||||
elif entry.suffix.lower() in SUPPORTED_EXTENSIONS or entry.name.lower() in ("dockerfile", "makefile"):
|
||||
items.append({
|
||||
"name": entry.name,
|
||||
"path": rel,
|
||||
"type": "file",
|
||||
"size": entry.stat().st_size,
|
||||
"extension": entry.suffix.lower(),
|
||||
})
|
||||
except PermissionError:
|
||||
raise ServiceError("Permission denied", code="permission_denied", status=403) from None
|
||||
|
||||
return {"vault": vault_name, "path": path, "items": items}
|
||||
|
||||
|
||||
def list_all_files(
|
||||
vault_name: str,
|
||||
dir: str = "",
|
||||
limit: int = 200,
|
||||
recursive: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""List files in a vault directory sorted by modification time (newest first).
|
||||
|
||||
Unlike :func:`browse_directory`, this returns files only (with metadata:
|
||||
size, mtime, extension) and can recurse into subdirectories. Hidden files
|
||||
and ignored directories are skipped.
|
||||
|
||||
Args:
|
||||
vault_name: Name of the vault.
|
||||
dir: Relative directory path within the vault (empty = root).
|
||||
limit: Maximum number of files to return.
|
||||
recursive: Recurse into subdirectories when True.
|
||||
|
||||
Returns:
|
||||
Dict with ``vault``, ``directory``, ``recursive``, ``count`` and
|
||||
``files``.
|
||||
|
||||
Raises:
|
||||
ServiceError: ``not_found`` (404) if the directory does not exist,
|
||||
``permission_denied`` (403) if it cannot be read.
|
||||
"""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from backend.indexer import IGNORED_DIRS
|
||||
|
||||
root = get_vault_root(vault_name)
|
||||
dir_path = resolve_safe_path(root, dir) if dir else root
|
||||
|
||||
if not dir_path.exists() or not dir_path.is_dir():
|
||||
raise ServiceError(
|
||||
f"Directory not found: {dir}",
|
||||
code="not_found",
|
||||
status=404,
|
||||
details={"vault": vault_name, "path": dir},
|
||||
)
|
||||
|
||||
files: list[dict[str, Any]] = []
|
||||
dir_prefix = (dir or "").strip("/")
|
||||
|
||||
try:
|
||||
iterator = dir_path.rglob("*") if recursive else dir_path.iterdir()
|
||||
for entry in iterator:
|
||||
if not entry.is_file():
|
||||
continue
|
||||
if entry.name.startswith("."):
|
||||
continue
|
||||
if entry.name in IGNORED_DIRS:
|
||||
continue
|
||||
|
||||
if recursive and dir_prefix:
|
||||
skip = False
|
||||
try:
|
||||
for part in entry.relative_to(dir_path).parts[:-1]:
|
||||
if part.startswith(".") or part in IGNORED_DIRS:
|
||||
skip = True
|
||||
break
|
||||
except ValueError:
|
||||
pass
|
||||
if skip:
|
||||
continue
|
||||
|
||||
stat = entry.stat()
|
||||
rel_path = str(entry.relative_to(root)).replace("\\", "/")
|
||||
|
||||
if recursive and dir_prefix:
|
||||
try:
|
||||
rel_to_dir = str(entry.parent.relative_to(dir_path)).replace("\\", "/")
|
||||
except ValueError:
|
||||
rel_to_dir = ""
|
||||
else:
|
||||
rel_to_dir = ""
|
||||
|
||||
ext = entry.suffix.lower() if entry.suffix else ""
|
||||
|
||||
file_entry = {
|
||||
"name": entry.name,
|
||||
"path": rel_path,
|
||||
"vault": vault_name,
|
||||
"size": stat.st_size,
|
||||
"modified": stat.st_mtime,
|
||||
"modified_iso": datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat(),
|
||||
"extension": ext.lstrip(".") if ext else "",
|
||||
}
|
||||
if rel_to_dir and rel_to_dir != ".":
|
||||
file_entry["rel_dir"] = rel_to_dir
|
||||
|
||||
files.append(file_entry)
|
||||
except PermissionError:
|
||||
raise ServiceError(
|
||||
"Permission denied reading directory",
|
||||
code="permission_denied",
|
||||
status=403,
|
||||
details={"vault": vault_name, "path": dir},
|
||||
) from None
|
||||
|
||||
files.sort(key=lambda f: f["modified"], reverse=True)
|
||||
files = files[:limit]
|
||||
|
||||
return {
|
||||
"vault": vault_name,
|
||||
"directory": dir,
|
||||
"recursive": recursive,
|
||||
"count": len(files),
|
||||
"files": files,
|
||||
}
|
||||
+19
-7
@@ -8,11 +8,10 @@ No authentication required for public share views.
|
||||
"""
|
||||
|
||||
import json
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional
|
||||
import logging
|
||||
import secrets
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger("obsigate.share")
|
||||
|
||||
@@ -39,7 +38,7 @@ def create_share(
|
||||
vault: str,
|
||||
path: str,
|
||||
created_by: str,
|
||||
expires_in_hours: Optional[int] = None,
|
||||
expires_in_hours: int | None = None,
|
||||
) -> dict:
|
||||
"""Create a new share token for a document."""
|
||||
data = _read()
|
||||
@@ -66,7 +65,7 @@ def create_share(
|
||||
return share
|
||||
|
||||
|
||||
def get_share_by_token(token: str) -> Optional[dict]:
|
||||
def get_share_by_token(token: str) -> dict | None:
|
||||
"""Look up a share by token. Returns None if expired or not found."""
|
||||
data = _read()
|
||||
share = data["shares"].get(token)
|
||||
@@ -100,7 +99,7 @@ def revoke_share(share_id: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def list_shares(vault_filter: Optional[str] = None) -> list:
|
||||
def list_shares(vault_filter: str | None = None) -> list:
|
||||
"""List all shares, optionally filtered by vault."""
|
||||
data = _read()
|
||||
shares = list(data["shares"].values())
|
||||
@@ -109,3 +108,16 @@ def list_shares(vault_filter: Optional[str] = None) -> list:
|
||||
# Most recent first
|
||||
shares.sort(key=lambda s: s.get("created_at", ""), reverse=True)
|
||||
return shares
|
||||
|
||||
|
||||
def update_shares_after_rename(vault: str, old_path: str, new_path: str):
|
||||
"""Update all shares when a file is renamed."""
|
||||
data = _read()
|
||||
updated = False
|
||||
for sid, s in data["shares"].items():
|
||||
if s.get("vault") == vault and s.get("path") == old_path:
|
||||
s["path"] = new_path
|
||||
updated = True
|
||||
logger.info(f"Updated share {sid}: {vault}/{old_path} -> {new_path}")
|
||||
if updated:
|
||||
_write(data)
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
"""AI assistant skills & slash-commands.
|
||||
|
||||
A *skill* is a reusable prompt/workflow the user can trigger from the assistant
|
||||
composer with ``/``. ObsiGate ships a set of built-in skills; users can create
|
||||
their own with ``/create-new-skill``. Built-in skills are code constants, while
|
||||
user skills are persisted per-user in ``data/skills.json``.
|
||||
|
||||
The same module also exposes the metadata for the *admin* commands
|
||||
(``/help``, ``/providers``, ``/provider``, ``/model``, ``/keys``). Those are
|
||||
executed client-side (they only touch the picker / display info), but listing
|
||||
them here keeps the ``/`` menu single-sourced.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.skills")
|
||||
|
||||
SKILLS_FILE = Path("data/skills.json")
|
||||
MAX_USER_SKILLS = 100
|
||||
MAX_PROMPT_CHARS = 8000
|
||||
_SKILL_ID_RE = re.compile(r"^[a-z0-9][a-z0-9_-]{0,47}$")
|
||||
|
||||
# ── Built-in skills ─────────────────────────────────────────────────────────
|
||||
# ``prompt`` is appended to the assistant system prompt when the skill is
|
||||
# selected. Keep prompts concise and language-agnostic: the model answers in
|
||||
# the user's language.
|
||||
BUILTIN_SKILLS: list[dict[str, Any]] = [
|
||||
{
|
||||
"id": "research",
|
||||
"label": "Recherche structurée",
|
||||
"icon": "🔎",
|
||||
"type": "skill",
|
||||
"description": "Recherche structurée + recommandation",
|
||||
"prompt": (
|
||||
"Applique un mode RECHERCHE STRUCTURÉE. Structure ta réponse en : "
|
||||
"1) Contexte et question reformulée, 2) Constats appuyés sur le contenu fourni, "
|
||||
"3) Options/approches avec avantages et limites, 4) Recommandation argumentée. "
|
||||
"Cite les sources (fichiers) utilisées."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "create-new-skill",
|
||||
"label": "Créer un skill",
|
||||
"icon": "🛠️",
|
||||
"type": "skill",
|
||||
"special": "create_skill",
|
||||
"description": "Crée un workflow réutilisable (skill)",
|
||||
"prompt": "",
|
||||
},
|
||||
{
|
||||
"id": "resume",
|
||||
"label": "Résumé",
|
||||
"icon": "📄",
|
||||
"type": "skill",
|
||||
"description": "Résumé / synthèse structurée",
|
||||
"prompt": (
|
||||
"Produis un RÉSUMÉ structuré du contenu : idées clés, points importants, "
|
||||
"conclusions. Utilise des titres et des puces concises."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "actions",
|
||||
"label": "Actions & to-dos",
|
||||
"icon": "✅",
|
||||
"type": "skill",
|
||||
"description": "Extraire les actions & to-dos",
|
||||
"prompt": (
|
||||
"Extrais les ACTIONS et TO-DOS du contenu. Rends une liste de tâches markdown "
|
||||
"`- [ ] ...`, avec responsable et échéance si mentionnés, sinon `(à préciser)`."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "reformuler",
|
||||
"label": "Reformuler",
|
||||
"icon": "✍️",
|
||||
"type": "skill",
|
||||
"description": "Réécriture clarté / ton",
|
||||
"prompt": (
|
||||
"RÉÉCRIS le contenu pour améliorer la clarté et le ton, en préservant le sens. "
|
||||
"Retourne uniquement le texte reformulé."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "correction",
|
||||
"label": "Correction",
|
||||
"icon": "🔤",
|
||||
"type": "skill",
|
||||
"description": "Correction grammaire / orthographe / style",
|
||||
"prompt": (
|
||||
"CORRIGE la grammaire, l'orthographe et le style. Retourne le texte corrigé, "
|
||||
"puis une courte liste des corrections notables."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "brainstorm",
|
||||
"label": "Brainstorm",
|
||||
"icon": "💡",
|
||||
"type": "skill",
|
||||
"description": "Générer des idées, angles, variantes",
|
||||
"prompt": (
|
||||
"Mode BRAINSTORM : génère un maximum d'idées, angles et variantes pertinents. "
|
||||
"Regroupe-les par thème, sans juger, puis signale les plus prometteuses."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "plan",
|
||||
"label": "Planifier",
|
||||
"icon": "🧭",
|
||||
"type": "skill",
|
||||
"description": "Planifier / structurer un document",
|
||||
"prompt": (
|
||||
"PLANIFIE et structure un document : propose un plan détaillé (sections, "
|
||||
"sous-sections, objectif de chaque partie) et une progression logique."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "ask",
|
||||
"label": "Q&R",
|
||||
"icon": "💬",
|
||||
"type": "skill",
|
||||
"description": "Q&A sur un contenu référencé",
|
||||
"prompt": (
|
||||
"Mode QUESTION/RÉPONSE : réponds précisément à la question en te basant "
|
||||
"strictement sur le contenu référencé. Cite les passages/fichiers utilisés et "
|
||||
"dis clairement si l'information est absente."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "meeting-note",
|
||||
"label": "Note de réunion",
|
||||
"icon": "📝",
|
||||
"type": "skill",
|
||||
"description": "Compte-rendu / note de réunion",
|
||||
"prompt": (
|
||||
"Rédige une NOTE DE RÉUNION : participants, ordre du jour, décisions, "
|
||||
"points d'action (`- [ ] ...`), questions ouvertes et prochaines étapes."
|
||||
),
|
||||
},
|
||||
{
|
||||
"id": "livrable",
|
||||
"label": "Livrable",
|
||||
"icon": "📨",
|
||||
"type": "skill",
|
||||
"description": "Email / compte-rendu / message Slack",
|
||||
"prompt": (
|
||||
"Rédige un LIVRABLE de communication (email, compte-rendu ou message Slack) "
|
||||
"clair et prêt à envoyer, adapté au canal et au destinataire indiqués."
|
||||
),
|
||||
},
|
||||
]
|
||||
|
||||
# ── Admin commands (handled client-side) ────────────────────────────────────
|
||||
ADMIN_COMMANDS: list[dict[str, Any]] = [
|
||||
{
|
||||
"id": "help",
|
||||
"label": "Aide",
|
||||
"icon": "❓",
|
||||
"type": "admin",
|
||||
"description": "Liste des commandes",
|
||||
},
|
||||
{
|
||||
"id": "providers",
|
||||
"label": "Fournisseurs",
|
||||
"icon": "🔌",
|
||||
"type": "admin",
|
||||
"description": "Liste les fournisseurs actifs",
|
||||
},
|
||||
{
|
||||
"id": "provider",
|
||||
"label": "Changer de fournisseur",
|
||||
"icon": "🔀",
|
||||
"type": "admin",
|
||||
"usage": "/provider <nom>",
|
||||
"description": "Changer de fournisseur LLM",
|
||||
},
|
||||
{
|
||||
"id": "model",
|
||||
"label": "Changer de modèle",
|
||||
"icon": "🧠",
|
||||
"type": "admin",
|
||||
"usage": "/model <nom>",
|
||||
"description": "Changer de modèle LLM",
|
||||
},
|
||||
{
|
||||
"id": "keys",
|
||||
"label": "Clés API",
|
||||
"icon": "🔑",
|
||||
"type": "admin",
|
||||
"description": "Fournisseurs avec clé API enregistrée",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def list_builtin_skills() -> list[dict[str, Any]]:
|
||||
"""Return a copy of the built-in skills."""
|
||||
return [dict(skill) for skill in BUILTIN_SKILLS]
|
||||
|
||||
|
||||
def list_admin_commands() -> list[dict[str, Any]]:
|
||||
"""Return a copy of the admin command metadata."""
|
||||
return [dict(cmd) for cmd in ADMIN_COMMANDS]
|
||||
|
||||
|
||||
def _read_store() -> dict[str, list[dict[str, Any]]]:
|
||||
if not SKILLS_FILE.exists():
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(SKILLS_FILE.read_text(encoding="utf-8"))
|
||||
return data if isinstance(data, dict) else {}
|
||||
except Exception as exc: # pragma: no cover - corrupted file
|
||||
logger.warning("Cannot read skills store: %s", exc)
|
||||
return {}
|
||||
|
||||
|
||||
def _write_store(store: dict[str, list[dict[str, Any]]]) -> None:
|
||||
SKILLS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = SKILLS_FILE.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(store, indent=2, ensure_ascii=False), encoding="utf-8")
|
||||
tmp.replace(SKILLS_FILE)
|
||||
|
||||
|
||||
def _username(user: dict | None) -> str:
|
||||
if not user:
|
||||
return "anonymous"
|
||||
return str(user.get("username") or "anonymous")
|
||||
|
||||
|
||||
def list_user_skills(user: dict | None) -> list[dict[str, Any]]:
|
||||
"""Return the persisted custom skills for a user."""
|
||||
store = _read_store()
|
||||
skills = store.get(_username(user), [])
|
||||
return [dict(s) for s in skills if isinstance(s, dict)]
|
||||
|
||||
|
||||
def list_skills(user: dict | None) -> dict[str, list[dict[str, Any]]]:
|
||||
"""Return built-in skills, admin commands and the user's custom skills."""
|
||||
return {
|
||||
"skills": list_builtin_skills() + list_user_skills(user),
|
||||
"commands": list_admin_commands(),
|
||||
}
|
||||
|
||||
|
||||
def get_skill_prompt(skill_id: str | None, user: dict | None) -> str | None:
|
||||
"""Resolve a skill id to its prompt, searching built-ins then user skills."""
|
||||
if not skill_id:
|
||||
return None
|
||||
for skill in BUILTIN_SKILLS:
|
||||
if skill["id"] == skill_id:
|
||||
return skill.get("prompt") or None
|
||||
for skill in list_user_skills(user):
|
||||
if skill.get("id") == skill_id:
|
||||
return skill.get("prompt") or None
|
||||
return None
|
||||
|
||||
|
||||
def create_user_skill(user: dict | None, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Create and persist a custom skill for a user.
|
||||
|
||||
Raises:
|
||||
ValueError: when the payload is invalid (bad id, duplicate, too many).
|
||||
"""
|
||||
skill_id = str(payload.get("id") or "").strip().lower()
|
||||
label = str(payload.get("label") or "").strip()
|
||||
prompt = str(payload.get("prompt") or "").strip()
|
||||
description = str(payload.get("description") or "").strip()
|
||||
|
||||
if not _SKILL_ID_RE.match(skill_id):
|
||||
raise ValueError("Identifiant invalide (a-z, 0-9, '-', '_', max 48)")
|
||||
if any(s["id"] == skill_id for s in BUILTIN_SKILLS):
|
||||
raise ValueError(f"L'identifiant '{skill_id}' est réservé")
|
||||
if not label:
|
||||
raise ValueError("Le nom du skill est requis")
|
||||
if not prompt:
|
||||
raise ValueError("Le prompt du skill est requis")
|
||||
if len(prompt) > MAX_PROMPT_CHARS:
|
||||
raise ValueError("Le prompt est trop long")
|
||||
|
||||
username = _username(user)
|
||||
store = _read_store()
|
||||
user_skills = store.get(username, [])
|
||||
if any(s.get("id") == skill_id for s in user_skills):
|
||||
raise ValueError(f"Le skill '{skill_id}' existe déjà")
|
||||
if len(user_skills) >= MAX_USER_SKILLS:
|
||||
raise ValueError("Trop de skills personnalisés")
|
||||
|
||||
skill = {
|
||||
"id": skill_id,
|
||||
"label": label,
|
||||
"icon": str(payload.get("icon") or "🧩").strip() or "🧩",
|
||||
"type": "skill",
|
||||
"custom": True,
|
||||
"description": description or label,
|
||||
"prompt": prompt,
|
||||
}
|
||||
user_skills.append(skill)
|
||||
store[username] = user_skills
|
||||
_write_store(store)
|
||||
return skill
|
||||
|
||||
|
||||
def delete_user_skill(user: dict | None, skill_id: str) -> bool:
|
||||
"""Delete a custom skill. Returns True when a skill was removed."""
|
||||
username = _username(user)
|
||||
store = _read_store()
|
||||
user_skills = store.get(username, [])
|
||||
remaining = [s for s in user_skills if s.get("id") != skill_id]
|
||||
if len(remaining) == len(user_skills):
|
||||
return False
|
||||
store[username] = remaining
|
||||
_write_store(store)
|
||||
return True
|
||||
@@ -0,0 +1,98 @@
|
||||
"""API routes for AI assistant skills & slash-commands (``/api/ai/skills``)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from backend.auth.middleware import require_auth
|
||||
from backend.skills import (
|
||||
create_user_skill,
|
||||
delete_user_skill,
|
||||
list_skills,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("obsigate.skills_routes")
|
||||
router = APIRouter(prefix="/api/ai/skills", tags=["AI"])
|
||||
|
||||
|
||||
class SkillModel(BaseModel):
|
||||
"""A single skill or admin command."""
|
||||
|
||||
model_config = {"extra": "allow"}
|
||||
|
||||
id: str
|
||||
label: str
|
||||
icon: str = "🧩"
|
||||
type: str = Field(default="skill", description="'skill' or 'admin'")
|
||||
description: str = ""
|
||||
prompt: str | None = None
|
||||
usage: str | None = None
|
||||
special: str | None = None
|
||||
custom: bool = False
|
||||
|
||||
|
||||
class SkillsResponse(BaseModel):
|
||||
"""Response for ``GET /api/ai/skills``."""
|
||||
|
||||
skills: list[SkillModel]
|
||||
commands: list[SkillModel]
|
||||
|
||||
|
||||
class CreateSkillRequest(BaseModel):
|
||||
"""Body for ``POST /api/ai/skills``."""
|
||||
|
||||
id: str = Field(description="Stable identifier (a-z, 0-9, '-', '_')")
|
||||
label: str = Field(description="Display name")
|
||||
prompt: str = Field(description="Instruction injected into the system prompt")
|
||||
description: str = ""
|
||||
icon: str = "🧩"
|
||||
|
||||
|
||||
class DeleteSkillResponse(BaseModel):
|
||||
"""Response for ``DELETE /api/ai/skills/{skill_id}``."""
|
||||
|
||||
status: str = "deleted"
|
||||
id: str
|
||||
|
||||
|
||||
@router.get("", response_model=SkillsResponse)
|
||||
async def api_list_skills(current_user=Depends(require_auth)):
|
||||
"""List built-in skills, admin commands and the user's custom skills."""
|
||||
data = list_skills(current_user)
|
||||
return data
|
||||
|
||||
|
||||
@router.post("", response_model=SkillModel)
|
||||
async def api_create_skill(body: CreateSkillRequest, current_user=Depends(require_auth)):
|
||||
"""Create a custom skill for the current user."""
|
||||
try:
|
||||
skill = create_user_skill(current_user, body.model_dump())
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return skill
|
||||
|
||||
|
||||
@router.delete("/{skill_id}", response_model=DeleteSkillResponse)
|
||||
async def api_delete_skill(skill_id: str, current_user=Depends(require_auth)):
|
||||
"""Delete a custom skill owned by the current user."""
|
||||
removed = delete_user_skill(current_user, skill_id)
|
||||
if not removed:
|
||||
raise HTTPException(status_code=404, detail="Skill introuvable")
|
||||
return {"status": "deleted", "id": skill_id}
|
||||
|
||||
|
||||
# Expose the resolved prompt of a skill (used by tests / clients that only
|
||||
# need the instruction text without fetching the whole list).
|
||||
@router.get("/{skill_id}/prompt")
|
||||
async def api_skill_prompt(skill_id: str, current_user=Depends(require_auth)) -> dict[str, Any]:
|
||||
"""Return the prompt text for a given skill id."""
|
||||
from backend.skills import get_skill_prompt
|
||||
|
||||
prompt = get_skill_prompt(skill_id, current_user)
|
||||
if prompt is None:
|
||||
raise HTTPException(status_code=404, detail="Skill introuvable")
|
||||
return {"id": skill_id, "prompt": prompt}
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Public facade for the AI tool layer.
|
||||
|
||||
Importing this module registers all built-in tools (via
|
||||
``backend.tools.service``) and re-exports the public API. Consumers (agent
|
||||
loop, MCP server, tests) should import from here rather than from individual
|
||||
submodules.
|
||||
|
||||
Note: ObsiGate uses implicit namespace packages (no tracked ``__init__.py``,
|
||||
which ``.gitignore`` excludes via ``_*.py``), hence this explicit facade.
|
||||
"""
|
||||
|
||||
from backend.tools import service as _service # noqa: F401 (registers tools)
|
||||
from backend.tools import web as _web # noqa: F401 (registers web tools)
|
||||
from backend.tools.context import (
|
||||
ToolConfirmationRequired,
|
||||
ToolContext,
|
||||
ToolError,
|
||||
ToolMode,
|
||||
ToolNotFoundError,
|
||||
ToolPermissionError,
|
||||
ToolRateLimitError,
|
||||
ToolRisk,
|
||||
ToolScope,
|
||||
ToolValidationError,
|
||||
resolve_safe_path,
|
||||
)
|
||||
from backend.tools.ratelimit import (
|
||||
check_and_record as check_tool_rate_limit,
|
||||
)
|
||||
from backend.tools.ratelimit import (
|
||||
get_status as get_tool_rate_limit_status,
|
||||
)
|
||||
from backend.tools.ratelimit import (
|
||||
reset as reset_tool_rate_limit,
|
||||
)
|
||||
from backend.tools.redaction import redact_payload
|
||||
from backend.tools.registry import (
|
||||
ToolSpec,
|
||||
call_tool,
|
||||
get_tool,
|
||||
get_tool_schemas,
|
||||
list_tools,
|
||||
tool,
|
||||
)
|
||||
from backend.tools.schemas import ToolResult
|
||||
|
||||
__all__ = [
|
||||
"ToolConfirmationRequired",
|
||||
"ToolContext",
|
||||
"ToolError",
|
||||
"ToolMode",
|
||||
"ToolNotFoundError",
|
||||
"ToolPermissionError",
|
||||
"ToolRateLimitError",
|
||||
"ToolResult",
|
||||
"ToolRisk",
|
||||
"ToolScope",
|
||||
"ToolSpec",
|
||||
"ToolValidationError",
|
||||
"call_tool",
|
||||
"check_tool_rate_limit",
|
||||
"get_tool",
|
||||
"get_tool_rate_limit_status",
|
||||
"get_tool_schemas",
|
||||
"list_tools",
|
||||
"redact_payload",
|
||||
"reset_tool_rate_limit",
|
||||
"resolve_safe_path",
|
||||
"tool",
|
||||
]
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Audit logging for AI tool calls.
|
||||
|
||||
Reuses the application audit log (``data/audit.log``, JSON lines) and adds an
|
||||
``ai_tool_call`` action. Argument values that may contain sensitive payloads
|
||||
(file content, prompts) are summarized rather than stored verbatim.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from backend.audit import _write_entry
|
||||
|
||||
# Argument keys whose values may contain secrets or large payloads.
|
||||
_SENSITIVE_ARG_KEYS = {"content", "text", "body"}
|
||||
_MAX_ARG_CHARS = 200
|
||||
|
||||
|
||||
def _sanitize_arguments(arguments: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""Return a log-safe view of tool arguments."""
|
||||
safe: dict[str, Any] = {}
|
||||
for key, value in (arguments or {}).items():
|
||||
if key in _SENSITIVE_ARG_KEYS:
|
||||
safe[key] = f"<{len(str(value))} chars>"
|
||||
else:
|
||||
safe[key] = str(value)[:_MAX_ARG_CHARS]
|
||||
return safe
|
||||
|
||||
|
||||
def log_tool_call(
|
||||
*,
|
||||
username: str,
|
||||
mode: str,
|
||||
tool: str,
|
||||
arguments: dict[str, Any] | None = None,
|
||||
ok: bool = True,
|
||||
vault: str | None = None,
|
||||
ip: str | None = None,
|
||||
error: str | None = None,
|
||||
) -> None:
|
||||
"""Append an ``ai_tool_call`` entry to the audit log."""
|
||||
_write_entry({
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"action": "ai_tool_call",
|
||||
"username": username,
|
||||
"ip": ip or "unknown",
|
||||
"mode": mode,
|
||||
"tool": tool,
|
||||
"vault": vault,
|
||||
"ok": ok,
|
||||
"error": error,
|
||||
"arguments": _sanitize_arguments(arguments),
|
||||
})
|
||||
@@ -0,0 +1,210 @@
|
||||
"""Execution context and shared primitives for the AI tool layer.
|
||||
|
||||
The tool layer is deliberately transport-agnostic: the same tool functions are
|
||||
invoked by the in-app assistant (function calling) and by the MCP server. This
|
||||
module holds the context object that carries the caller identity, the
|
||||
permission helpers, and the domain error types shared across the layer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from backend.auth.middleware import check_vault_access
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.services.paths import resolve_safe_path as _resolve_service_path
|
||||
|
||||
logger = logging.getLogger("obsigate.tools")
|
||||
|
||||
|
||||
class ToolMode(str, Enum):
|
||||
"""How a tool was invoked."""
|
||||
|
||||
IN_APP = "in_app"
|
||||
MCP = "mcp"
|
||||
|
||||
|
||||
class ToolScope(str, Enum):
|
||||
"""Which front a tool is exposed to."""
|
||||
|
||||
IN_APP = "in_app"
|
||||
MCP = "mcp"
|
||||
|
||||
|
||||
class ToolRisk(str, Enum):
|
||||
"""Risk level of a tool.
|
||||
|
||||
``READ`` tools run without confirmation. ``WRITE`` and ``DANGEROUS`` tools
|
||||
require an explicit confirmation (two-step ``propose``/``apply`` for MCP,
|
||||
an "Apply" card in the in-app UI).
|
||||
"""
|
||||
|
||||
READ = "read"
|
||||
WRITE = "write"
|
||||
DANGEROUS = "dangerous"
|
||||
|
||||
|
||||
class ToolError(Exception):
|
||||
"""Base class for tool execution errors.
|
||||
|
||||
Carries a stable, machine-readable ``code`` so callers (agent loop, MCP
|
||||
server) can react without parsing the message.
|
||||
"""
|
||||
|
||||
code = "tool_error"
|
||||
|
||||
def __init__(self, message: str, *, code: str | None = None, details: dict[str, Any] | None = None):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
if code:
|
||||
self.code = code
|
||||
self.details = details or {}
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {"ok": False, "error": {"code": self.code, "message": self.message, "details": self.details}}
|
||||
|
||||
|
||||
class ToolNotFoundError(ToolError):
|
||||
code = "not_found"
|
||||
|
||||
|
||||
class ToolPermissionError(ToolError):
|
||||
code = "permission_denied"
|
||||
|
||||
|
||||
class ToolValidationError(ToolError):
|
||||
code = "invalid_arguments"
|
||||
|
||||
|
||||
class ToolConfirmationRequired(ToolError):
|
||||
"""Raised when a mutating tool is invoked without confirmation.
|
||||
|
||||
This is the hook for the two-step ``propose``/``apply`` mechanism: the
|
||||
``propose`` phase surfaces this payload to the user, and the ``apply``
|
||||
phase re-invokes the tool with ``confirm=True``.
|
||||
"""
|
||||
|
||||
code = "confirmation_required"
|
||||
|
||||
def __init__(self, tool: str, arguments: dict[str, Any], message: str | None = None):
|
||||
super().__init__(message or f"Confirmation required for '{tool}'", code="confirmation_required")
|
||||
self.tool = tool
|
||||
self.arguments = arguments
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
payload = super().to_dict()
|
||||
payload["error"]["tool"] = self.tool
|
||||
payload["error"]["arguments"] = self.arguments
|
||||
return payload
|
||||
|
||||
|
||||
class ToolRateLimitError(ToolError):
|
||||
"""Raised when an identity exceeds its tool-call rate limit (Phase F)."""
|
||||
|
||||
code = "rate_limited"
|
||||
|
||||
def __init__(self, tool: str, retry_after: int = 0, message: str | None = None):
|
||||
super().__init__(
|
||||
message or f"Rate limit exceeded for tool '{tool}'",
|
||||
code="rate_limited",
|
||||
details={"tool": tool, "retry_after": retry_after},
|
||||
)
|
||||
self.tool = tool
|
||||
self.retry_after = retry_after
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolContext:
|
||||
"""Carries the caller identity and execution options for a tool call.
|
||||
|
||||
Attributes:
|
||||
user: Authenticated user dict (as returned by ``get_current_user``).
|
||||
mode: Whether the call originates from the in-app assistant or MCP.
|
||||
confirmed: True once a mutating action has been approved.
|
||||
ip: Optional client IP for auditing.
|
||||
audit_enabled: Set to False to skip audit logging (tests, dry runs).
|
||||
metadata: Free-form caller metadata (conversation id, client name…).
|
||||
"""
|
||||
|
||||
user: dict[str, Any]
|
||||
mode: ToolMode = ToolMode.IN_APP
|
||||
confirmed: bool = False
|
||||
ip: str | None = None
|
||||
audit_enabled: bool = True
|
||||
metadata: dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
@property
|
||||
def username(self) -> str:
|
||||
return self.user.get("username", "unknown")
|
||||
|
||||
@property
|
||||
def rate_limit_identity(self) -> str:
|
||||
"""Identity used by the per-token/per-tool rate limiter.
|
||||
|
||||
Prefers the JWT id (``_token_jti``, attached by the auth middleware),
|
||||
then an explicit ``token_id`` in :attr:`metadata`, then the user id and
|
||||
finally the username. This yields per-token limiting when a token id is
|
||||
available and per-account limiting otherwise.
|
||||
"""
|
||||
token_id = self.metadata.get("token_id") or self.user.get("_token_jti")
|
||||
if token_id:
|
||||
return f"token:{token_id}"
|
||||
user_id = self.user.get("id") or self.user.get("username")
|
||||
return f"user:{user_id or 'anonymous'}"
|
||||
|
||||
def has_vault_access(self, vault: str) -> bool:
|
||||
"""Return True if the caller may access *vault*."""
|
||||
return bool(vault) and check_vault_access(vault, self.user)
|
||||
|
||||
def require_vault_access(self, vault: str) -> None:
|
||||
"""Raise :class:`ToolPermissionError` unless the caller can access *vault*."""
|
||||
if not self.has_vault_access(vault):
|
||||
raise ToolPermissionError(
|
||||
f"Access denied to vault '{vault}'",
|
||||
code="vault_access_denied",
|
||||
details={"vault": vault},
|
||||
)
|
||||
|
||||
def destructive_tools_enabled(self, vault: str) -> bool:
|
||||
"""Return whether destructive tools are allowed for *vault*.
|
||||
|
||||
Controlled by the per-vault ``aiDestructiveTools`` setting (default:
|
||||
enabled). Disabling it blocks delete/rename/move/find-replace tools
|
||||
while keeping create/edit/append available.
|
||||
"""
|
||||
from backend.vault_settings import get_vault_setting
|
||||
|
||||
settings = get_vault_setting(vault) or {}
|
||||
return bool(settings.get("aiDestructiveTools", True))
|
||||
|
||||
def require_destructive_allowed(self, vault: str) -> None:
|
||||
"""Raise :class:`ToolPermissionError` if destructive tools are disabled."""
|
||||
if not self.destructive_tools_enabled(vault):
|
||||
raise ToolPermissionError(
|
||||
f"Destructive tools are disabled for vault '{vault}'",
|
||||
code="destructive_tools_disabled",
|
||||
details={"vault": vault},
|
||||
)
|
||||
|
||||
|
||||
def resolve_safe_path(vault_root: Path, relative_path: str | None) -> Path:
|
||||
"""Resolve a vault-relative path, rejecting traversal outside the vault.
|
||||
|
||||
Delegates to the shared :func:`backend.services.paths.resolve_safe_path`
|
||||
and maps its :class:`ServiceError` to tool domain errors so the tool layer
|
||||
stays transport-agnostic.
|
||||
|
||||
Raises:
|
||||
ToolPermissionError: When the resolved path escapes the vault root.
|
||||
ToolError: When the path cannot be resolved.
|
||||
"""
|
||||
try:
|
||||
return _resolve_service_path(vault_root, relative_path)
|
||||
except ServiceError as e:
|
||||
if e.code in ("path_outside_vault", "permission_denied"):
|
||||
raise ToolPermissionError(e.message, code=e.code, details=e.details) from e
|
||||
raise ToolError(e.message, code=e.code, details=e.details) from e
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Human-readable labels for tool calls (Notion-style “steps” section).
|
||||
|
||||
The assistant's conversation window shows a collapsible « N steps » block
|
||||
above each answer. Raw tool names (``search_fulltext``) are developer-centric;
|
||||
this module maps every registered tool to an i18n message key plus the
|
||||
argument worth surfacing (path, query, …), so the UI can render sentences like
|
||||
« Recherche dans le vault : pizza » or « Fichier lu : notes/x.md ».
|
||||
|
||||
The label KEY is emitted with each ``tool`` SSE event; the client resolves it
|
||||
against its locale dictionaries (``ai.step.<key>``). Unknown tools fall back to
|
||||
a generic key with the raw name prettified, so a new tool never breaks the UI.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
# tool name -> (message-key, primary argument name or None)
|
||||
# NOTE: argument names MUST match the pydantic input models in
|
||||
# backend/tools/schemas.py (verified by tests/test_tool_labels.py).
|
||||
_STEP_LABELS: dict[str, tuple[str, str | None]] = {
|
||||
"list_vaults": ("vaults", None),
|
||||
"list_directory": ("directory", "path"),
|
||||
"list_all_files": ("files", "dir"),
|
||||
"read_file": ("file_read", "path"),
|
||||
"read_file_raw": ("file_read", "path"),
|
||||
"get_backlinks": ("backlinks", "path"),
|
||||
"list_backups": ("backups", "path"),
|
||||
"diff_backup": ("backup_diff", "path"),
|
||||
"get_graph": ("graph", None),
|
||||
"search_fulltext": ("search", "q"),
|
||||
"search_advanced": ("search", "q"),
|
||||
"search_paths": ("search_paths", "q"),
|
||||
"list_tags": ("tags", None),
|
||||
"suggest_tags": ("tags_suggest", "q"),
|
||||
"list_recent": ("recent", None),
|
||||
"create_file": ("file_create", "path"),
|
||||
"create_directory": ("dir_create", "path"),
|
||||
"edit_file": ("file_edit", "path"),
|
||||
"append_to_file": ("file_append", "path"),
|
||||
"rename_file": ("file_rename", "path"),
|
||||
"rename_directory": ("dir_rename", "path"),
|
||||
"move_path": ("move", "source_path"),
|
||||
"replace_in_files": ("replace", "find"),
|
||||
"delete_file": ("file_delete", "path"),
|
||||
"delete_directory": ("dir_delete", "path"),
|
||||
"restore_backup": ("backup_restore", "path"),
|
||||
"web_search": ("web_search", "query"),
|
||||
"fetch_url": ("fetch_url", "url"),
|
||||
}
|
||||
|
||||
GENERIC_KEY = "generic"
|
||||
|
||||
|
||||
def _prettify(name: str) -> str:
|
||||
return name.replace("_", " ").strip()
|
||||
|
||||
|
||||
def _primary_value(arguments: dict[str, Any], arg: str | None) -> str | None:
|
||||
if not arg:
|
||||
return None
|
||||
value = arguments.get(arg)
|
||||
if isinstance(value, str) and value.strip():
|
||||
return value.strip()
|
||||
return None
|
||||
|
||||
|
||||
def tool_step_label(name: str, arguments: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
"""Return ``{key, params}`` describing one tool call in human terms.
|
||||
|
||||
``key`` resolves client-side to ``ai.step.<key>``; ``params`` carries the
|
||||
optional ``{value}`` placeholder (path, query, …). For an unknown tool the
|
||||
generic key is used with the prettified name as the value.
|
||||
"""
|
||||
arguments = arguments or {}
|
||||
key, arg = _STEP_LABELS.get(name, (GENERIC_KEY, None))
|
||||
if key == GENERIC_KEY:
|
||||
return {"key": GENERIC_KEY, "params": {"tool": _prettify(name)}}
|
||||
value = _primary_value(arguments, arg)
|
||||
params: dict[str, str] = {}
|
||||
if value is not None:
|
||||
params["value"] = value
|
||||
return {"key": key, "params": params}
|
||||
|
||||
|
||||
def thought_step_label(text: str) -> dict[str, Any]:
|
||||
"""Step descriptor for one intermediate reasoning note of the model.
|
||||
|
||||
The note is shown expanded under a “Thought” sub-section in the UI, so it
|
||||
is kept long enough to be readable (truncation is the safety net).
|
||||
"""
|
||||
cleaned = " ".join((text or "").split())
|
||||
return {"key": "thought", "params": {"value": cleaned[:1200]}}
|
||||
@@ -0,0 +1,153 @@
|
||||
"""Rate limiting for the AI tool layer (Phase F).
|
||||
|
||||
Complements the IP-based login limiter (``backend.ratelimit``) with a
|
||||
per-identity, per-tool sliding-window limiter applied to every tool call,
|
||||
whether it originates from the in-app assistant or the MCP server.
|
||||
|
||||
Two counters are maintained per identity:
|
||||
|
||||
* a **global** counter (all tools combined), capped by
|
||||
``OBSIGATE_TOOL_RATE_LIMIT``;
|
||||
* a **per-tool** counter, capped by ``OBSIGATE_TOOL_RATE_LIMIT_PER_TOOL``
|
||||
(defaults to the global cap) so a single expensive tool cannot consume the
|
||||
whole budget.
|
||||
|
||||
The window length is ``OBSIGATE_TOOL_RATE_WINDOW`` seconds (default 60).
|
||||
|
||||
The identity is the token JTI when available (``_token_jti`` attached by the
|
||||
auth middleware), otherwise the user id/username. This gives "per token / per
|
||||
tool" limiting while remaining meaningful for anonymous/disabled-auth mode.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from collections import deque
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.tools.ratelimit")
|
||||
|
||||
# --- Configuration (read at call time so tests can monkeypatch env) ---
|
||||
DEFAULT_RATE_LIMIT = 60
|
||||
DEFAULT_RATE_WINDOW = 60
|
||||
|
||||
|
||||
def _env_int(name: str, default: int) -> int:
|
||||
try:
|
||||
return int(os.environ.get(name, str(default)))
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _global_limit() -> int:
|
||||
return _env_int("OBSIGATE_TOOL_RATE_LIMIT", DEFAULT_RATE_LIMIT)
|
||||
|
||||
|
||||
def _per_tool_limit() -> int:
|
||||
return _env_int("OBSIGATE_TOOL_RATE_LIMIT_PER_TOOL", _global_limit())
|
||||
|
||||
|
||||
def _window() -> int:
|
||||
return max(1, _env_int("OBSIGATE_TOOL_RATE_WINDOW", DEFAULT_RATE_WINDOW))
|
||||
|
||||
|
||||
# --- In-memory store: {counter_key: deque[timestamp]} ---
|
||||
_calls: dict[str, deque] = {}
|
||||
|
||||
|
||||
def _identity_key(identity: str | None) -> str:
|
||||
return identity or "anonymous"
|
||||
|
||||
|
||||
def _counter_key(identity: str | None, tool: str | None) -> str:
|
||||
base = _identity_key(identity)
|
||||
return f"{base}\x00{tool}" if tool else base
|
||||
|
||||
|
||||
def _prune(entries: deque, now: float, window: int) -> None:
|
||||
cutoff = now - window
|
||||
while entries and entries[0] <= cutoff:
|
||||
entries.popleft()
|
||||
|
||||
|
||||
def check_and_record(identity: str | None, tool: str, *, now: float | None = None) -> tuple[bool, int]:
|
||||
"""Record one call and report whether it is allowed.
|
||||
|
||||
Args:
|
||||
identity: Rate-limit identity (token JTI or username).
|
||||
tool: Tool name (used for the per-tool counter).
|
||||
now: Optional timestamp override (tests).
|
||||
|
||||
Returns:
|
||||
``(allowed, retry_after)``. When ``allowed`` is False, ``retry_after``
|
||||
is the number of seconds until the oldest call leaves the window.
|
||||
"""
|
||||
now = time.time() if now is None else now
|
||||
window = _window()
|
||||
global_limit = _global_limit()
|
||||
tool_limit = _per_tool_limit()
|
||||
|
||||
global_entries = _calls.setdefault(_counter_key(identity, None), deque())
|
||||
tool_entries = _calls.setdefault(_counter_key(identity, tool), deque())
|
||||
_prune(global_entries, now, window)
|
||||
_prune(tool_entries, now, window)
|
||||
|
||||
if len(global_entries) >= global_limit or len(tool_entries) >= tool_limit:
|
||||
oldest = min(
|
||||
global_entries[0] if len(global_entries) >= global_limit else now,
|
||||
tool_entries[0] if len(tool_entries) >= tool_limit else now,
|
||||
)
|
||||
retry_after = max(1, int(oldest + window - now) + 1)
|
||||
logger.warning(f"Tool rate limit exceeded for '{_identity_key(identity)}' on '{tool}'")
|
||||
return False, retry_after
|
||||
|
||||
global_entries.append(now)
|
||||
tool_entries.append(now)
|
||||
return True, 0
|
||||
|
||||
|
||||
def remaining(identity: str | None, tool: str | None = None) -> int:
|
||||
"""Return the number of calls still allowed in the current window."""
|
||||
now = time.time()
|
||||
window = _window()
|
||||
if tool:
|
||||
entries = _calls.get(_counter_key(identity, tool))
|
||||
if entries is None:
|
||||
return _per_tool_limit()
|
||||
_prune(entries, now, window)
|
||||
return max(0, _per_tool_limit() - len(entries))
|
||||
entries = _calls.get(_counter_key(identity, None))
|
||||
if entries is None:
|
||||
return _global_limit()
|
||||
_prune(entries, now, window)
|
||||
return max(0, _global_limit() - len(entries))
|
||||
|
||||
|
||||
def reset(identity: str | None = None) -> None:
|
||||
"""Clear rate-limit state (all identities, or a single one)."""
|
||||
if identity is None:
|
||||
_calls.clear()
|
||||
return
|
||||
prefix = f"{_identity_key(identity)}\x00"
|
||||
for key in [k for k in _calls if k == _identity_key(identity) or k.startswith(prefix)]:
|
||||
del _calls[key]
|
||||
|
||||
|
||||
def get_status(identity: str | None = None) -> dict[str, Any]:
|
||||
"""Diagnostic snapshot of the limiter (for tests / admin tooling)."""
|
||||
if identity is None:
|
||||
return {
|
||||
"tracked_identities": len({k.split("\x00", 1)[0] for k in _calls}),
|
||||
"global_limit": _global_limit(),
|
||||
"per_tool_limit": _per_tool_limit(),
|
||||
"window_seconds": _window(),
|
||||
}
|
||||
return {
|
||||
"identity": _identity_key(identity),
|
||||
"remaining_global": remaining(identity),
|
||||
"global_limit": _global_limit(),
|
||||
"per_tool_limit": _per_tool_limit(),
|
||||
"window_seconds": _window(),
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Secret redaction for tool results (Phase F).
|
||||
|
||||
Tool results are fed back to the LLM (in-app agent loop) or returned to an
|
||||
external MCP client, so they must never leak credentials. ``read_file`` and
|
||||
``read_file_raw`` already redact through the shared file service, but other
|
||||
tools (``diff_backup``, ``search_*``) return content that has not been through
|
||||
the redactor. This module applies :func:`backend.secret_redactor.redact` to
|
||||
every string in a tool payload, recursively.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.secret_redactor import redact
|
||||
|
||||
_MAX_DEPTH = 12
|
||||
|
||||
|
||||
def redact_payload(payload: Any, _depth: int = 0) -> Any:
|
||||
"""Return a copy of *payload* with every string secret-redacted.
|
||||
|
||||
Walks dicts, lists and tuples; scalars are returned unchanged. Strings are
|
||||
run through :func:`backend.secret_redactor.redact`. A recursion cap avoids
|
||||
pathological/cyclic structures (tool results are JSON-serialisable, so
|
||||
cycles should not occur, but the guard keeps this safe).
|
||||
"""
|
||||
if _depth > _MAX_DEPTH:
|
||||
return payload
|
||||
if isinstance(payload, str):
|
||||
redacted, count = redact(payload)
|
||||
return redacted if count else payload
|
||||
if isinstance(payload, dict):
|
||||
return {key: redact_payload(value, _depth + 1) for key, value in payload.items()}
|
||||
if isinstance(payload, list):
|
||||
return [redact_payload(item, _depth + 1) for item in payload]
|
||||
if isinstance(payload, tuple):
|
||||
return tuple(redact_payload(item, _depth + 1) for item in payload)
|
||||
return payload
|
||||
@@ -0,0 +1,237 @@
|
||||
"""Tool registry — declarative registration and uniform execution.
|
||||
|
||||
Tools are registered with the :func:`tool` decorator. Each tool declares its
|
||||
input model (used both for JSON Schema exposure and argument validation), its
|
||||
risk level, and the fronts (in-app / MCP) it is exposed to.
|
||||
|
||||
Execution goes through :func:`call_tool`, which centralizes argument
|
||||
validation, vault permission checks, confirmation gating, and audit logging.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
from backend.services.errors import ServiceError
|
||||
from backend.tools.audit import log_tool_call
|
||||
from backend.tools.context import (
|
||||
ToolConfirmationRequired,
|
||||
ToolContext,
|
||||
ToolError,
|
||||
ToolNotFoundError,
|
||||
ToolPermissionError,
|
||||
ToolRateLimitError,
|
||||
ToolRisk,
|
||||
ToolScope,
|
||||
ToolValidationError,
|
||||
)
|
||||
from backend.tools.ratelimit import check_and_record
|
||||
from backend.tools.redaction import redact_payload
|
||||
from backend.tools.schemas import ToolResult
|
||||
|
||||
logger = logging.getLogger("obsigate.tools.registry")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ToolSpec:
|
||||
"""Static description of a registered tool."""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
input_model: type[BaseModel]
|
||||
handler: Callable[[ToolContext, Any], Any]
|
||||
risk: ToolRisk = ToolRisk.READ
|
||||
scopes: tuple[ToolScope, ...] = (ToolScope.IN_APP, ToolScope.MCP)
|
||||
requires_vault: bool = False
|
||||
|
||||
@property
|
||||
def requires_confirmation(self) -> bool:
|
||||
"""Mutating tools always require an explicit confirmation."""
|
||||
return self.risk != ToolRisk.READ
|
||||
|
||||
def parameters_schema(self) -> dict[str, Any]:
|
||||
"""JSON Schema of the tool arguments (for LLM function calling)."""
|
||||
schema = self.input_model.model_json_schema()
|
||||
schema.pop("title", None)
|
||||
return schema
|
||||
|
||||
def openai_schema(self) -> dict[str, Any]:
|
||||
"""OpenAI-compatible function-calling schema."""
|
||||
return {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": self.name,
|
||||
"description": self.description,
|
||||
"parameters": self.parameters_schema(),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
_REGISTRY: dict[str, ToolSpec] = {}
|
||||
|
||||
|
||||
def tool(
|
||||
*,
|
||||
name: str,
|
||||
description: str,
|
||||
input_model: type[BaseModel],
|
||||
risk: ToolRisk = ToolRisk.READ,
|
||||
scopes: tuple[ToolScope, ...] = (ToolScope.IN_APP, ToolScope.MCP),
|
||||
requires_vault: bool = False,
|
||||
) -> Callable[[Callable[[ToolContext, Any], Any]], Callable[[ToolContext, Any], Any]]:
|
||||
"""Register a tool. Returns the original handler unchanged."""
|
||||
|
||||
def decorator(func: Callable[[ToolContext, Any], Any]) -> Callable[[ToolContext, Any], Any]:
|
||||
if name in _REGISTRY:
|
||||
raise ValueError(f"Duplicate tool name: {name}")
|
||||
_REGISTRY[name] = ToolSpec(
|
||||
name=name,
|
||||
description=description,
|
||||
input_model=input_model,
|
||||
handler=func,
|
||||
risk=risk,
|
||||
scopes=scopes,
|
||||
requires_vault=requires_vault,
|
||||
)
|
||||
return func
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def get_tool(name: str) -> ToolSpec | None:
|
||||
"""Return the spec for *name*, or ``None``."""
|
||||
return _REGISTRY.get(name)
|
||||
|
||||
|
||||
def list_tools(*, scope: ToolScope | None = None) -> list[ToolSpec]:
|
||||
"""Return registered tools, optionally filtered by scope."""
|
||||
specs = list(_REGISTRY.values())
|
||||
if scope is not None:
|
||||
specs = [s for s in specs if scope in s.scopes]
|
||||
return specs
|
||||
|
||||
|
||||
def get_tool_schemas(*, scope: ToolScope | None = None) -> list[dict[str, Any]]:
|
||||
"""Return OpenAI-compatible schemas for registered tools."""
|
||||
return [spec.openai_schema() for spec in list_tools(scope=scope)]
|
||||
|
||||
|
||||
def _audit(ctx: ToolContext, spec: ToolSpec, arguments: dict[str, Any], *, ok: bool, error: str | None = None) -> None:
|
||||
if not ctx.audit_enabled:
|
||||
return
|
||||
try:
|
||||
log_tool_call(
|
||||
username=ctx.username,
|
||||
mode=ctx.mode.value,
|
||||
tool=spec.name,
|
||||
arguments=arguments,
|
||||
ok=ok,
|
||||
vault=arguments.get("vault"),
|
||||
ip=ctx.ip,
|
||||
error=error,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(f"Tool audit failed for '{spec.name}': {e}")
|
||||
|
||||
|
||||
def _map_service_error(e: ServiceError) -> ToolError:
|
||||
"""Map a shared-layer :class:`ServiceError` to a tool domain error."""
|
||||
if e.code == "not_found":
|
||||
return ToolNotFoundError(e.message, details=e.details)
|
||||
if e.code in ("permission_denied", "path_outside_vault", "vault_access_denied"):
|
||||
return ToolPermissionError(e.message, code=e.code, details=e.details)
|
||||
if e.code == "invalid_arguments":
|
||||
return ToolValidationError(e.message, details=e.details)
|
||||
return ToolError(e.message, code=e.code, details=e.details)
|
||||
|
||||
|
||||
def call_tool(
|
||||
name: str,
|
||||
ctx: ToolContext,
|
||||
arguments: dict[str, Any] | None = None,
|
||||
*,
|
||||
confirm: bool = False,
|
||||
) -> ToolResult:
|
||||
"""Validate, authorize, execute and audit a tool call.
|
||||
|
||||
Args:
|
||||
name: Registered tool name.
|
||||
ctx: Caller context (identity, mode, confirmation state).
|
||||
arguments: Raw arguments to validate against the tool input model.
|
||||
confirm: Explicit one-shot confirmation (two-step ``apply`` phase).
|
||||
|
||||
Raises:
|
||||
ToolNotFoundError: Unknown tool.
|
||||
ToolValidationError: Arguments fail validation.
|
||||
ToolPermissionError: Vault access denied or path escapes the vault.
|
||||
ToolConfirmationRequired: Mutating tool invoked without confirmation.
|
||||
ToolError: Any other execution error.
|
||||
"""
|
||||
spec = _REGISTRY.get(name)
|
||||
if spec is None:
|
||||
raise ToolNotFoundError(f"Unknown tool: {name}", details={"tool": name})
|
||||
|
||||
arguments = arguments or {}
|
||||
|
||||
try:
|
||||
params = spec.input_model.model_validate(arguments)
|
||||
except ValidationError as e:
|
||||
error = ToolValidationError(
|
||||
f"Invalid arguments for '{name}'",
|
||||
details={"errors": e.errors(include_url=False)},
|
||||
)
|
||||
_audit(ctx, spec, arguments, ok=False, error=error.code)
|
||||
raise error from e
|
||||
|
||||
allowed, retry_after = check_and_record(ctx.rate_limit_identity, spec.name)
|
||||
if not allowed:
|
||||
rate_error = ToolRateLimitError(spec.name, retry_after)
|
||||
_audit(ctx, spec, arguments, ok=False, error=rate_error.code)
|
||||
raise rate_error
|
||||
|
||||
vault = getattr(params, "vault", None)
|
||||
if spec.requires_vault and vault:
|
||||
try:
|
||||
ctx.require_vault_access(vault)
|
||||
except ToolPermissionError as e:
|
||||
_audit(ctx, spec, arguments, ok=False, error=e.code)
|
||||
raise
|
||||
|
||||
if spec.risk == ToolRisk.DANGEROUS and vault and vault != "all":
|
||||
try:
|
||||
ctx.require_destructive_allowed(vault)
|
||||
except ToolPermissionError as e:
|
||||
_audit(ctx, spec, arguments, ok=False, error=e.code)
|
||||
raise
|
||||
|
||||
if spec.requires_confirmation and not (confirm or ctx.confirmed):
|
||||
raise ToolConfirmationRequired(spec.name, arguments)
|
||||
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
data = spec.handler(ctx, params)
|
||||
except ToolError as e:
|
||||
_audit(ctx, spec, arguments, ok=False, error=e.code)
|
||||
raise
|
||||
except ServiceError as e:
|
||||
mapped = _map_service_error(e)
|
||||
_audit(ctx, spec, arguments, ok=False, error=mapped.code)
|
||||
raise mapped from e
|
||||
except Exception as e:
|
||||
logger.error(f"Tool '{name}' failed: {e}")
|
||||
exec_error = ToolError(f"Tool '{name}' failed: {e}", code="tool_execution_error")
|
||||
_audit(ctx, spec, arguments, ok=False, error=exec_error.code)
|
||||
raise exec_error from e
|
||||
|
||||
duration_ms = round((time.perf_counter() - started) * 1000, 2)
|
||||
logger.debug(f"Tool '{name}' ok in {duration_ms}ms")
|
||||
_audit(ctx, spec, arguments, ok=True)
|
||||
# Never forward raw secrets to the model (defense in depth: some tools —
|
||||
# diffs, search snippets — return content that was not pre-redacted).
|
||||
return ToolResult(ok=True, data=redact_payload(data))
|
||||
@@ -0,0 +1,261 @@
|
||||
"""Pydantic input/output models for the AI tool layer.
|
||||
|
||||
Input models double as JSON Schemas advertised to LLMs (via
|
||||
``model_json_schema``) and as validation for arguments received over MCP.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class ListVaultsInput(BaseModel):
|
||||
"""No parameters — lists the vaults the caller can access."""
|
||||
|
||||
|
||||
class ListDirectoryInput(BaseModel):
|
||||
"""Browse a directory inside a vault."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field("", description="Vault-relative directory path (empty = vault root)")
|
||||
|
||||
|
||||
class ReadFileInput(BaseModel):
|
||||
"""Read a text file's content from a vault."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
|
||||
|
||||
class SearchFulltextInput(BaseModel):
|
||||
"""Full-text search across one or all accessible vaults."""
|
||||
|
||||
q: str = Field(..., min_length=1, description="Search query")
|
||||
vault: str = Field("all", description="Vault name or 'all'")
|
||||
tag: str | None = Field(None, description="Optional comma-separated tag filter")
|
||||
limit: int = Field(50, ge=1, le=200, description="Maximum number of results")
|
||||
|
||||
|
||||
class ListTagsInput(BaseModel):
|
||||
"""List tags and their occurrence counts."""
|
||||
|
||||
vault: str | None = Field(None, description="Vault name or 'all' (default: all accessible)")
|
||||
|
||||
|
||||
class ListAllFilesInput(BaseModel):
|
||||
"""List every file of a vault (optionally under a subdirectory)."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
dir: str = Field("", description="Vault-relative directory path (empty = vault root)")
|
||||
limit: int = Field(200, ge=1, le=2000, description="Maximum number of files")
|
||||
recursive: bool = Field(True, description="Recurse into subdirectories")
|
||||
|
||||
|
||||
class ReadFileRawInput(BaseModel):
|
||||
"""Read a text file's raw content (no HTML rendering)."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
|
||||
|
||||
class GetBacklinksInput(BaseModel):
|
||||
"""List files linking to a target file via wikilinks."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative path of the target file")
|
||||
|
||||
|
||||
class ListBackupsInput(BaseModel):
|
||||
"""List available backup versions of a file."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
|
||||
|
||||
class DiffBackupInput(BaseModel):
|
||||
"""Unified diff between a backup version and another version or the current file."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
version: int = Field(..., description="Backup timestamp used as the old/left side")
|
||||
compare_with: int | None = Field(
|
||||
None,
|
||||
description="Backup timestamp used as the new/right side (omit to compare with the current file)",
|
||||
)
|
||||
|
||||
|
||||
class GetGraphInput(BaseModel):
|
||||
"""Graph data (nodes and edges) for a vault or directory."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field("", description="Vault-relative directory path to focus on (empty = root)")
|
||||
depth: int = Field(1, ge=0, le=3, description="Expansion depth")
|
||||
scope: str = Field("directory", description="'directory' for a subtree, 'full' for the whole vault")
|
||||
tag: str = Field("", description="Only include files carrying this tag")
|
||||
|
||||
|
||||
class SearchAdvancedInput(BaseModel):
|
||||
"""Advanced full-text search (TF-IDF, facets, operators)."""
|
||||
|
||||
q: str = Field("", description="Query (supports tag:, vault:, title:, path:, ext: operators)")
|
||||
vault: str = Field("all", description="Vault name or 'all'")
|
||||
tag: str | None = Field(None, description="Optional comma-separated tag filter")
|
||||
limit: int = Field(50, ge=1, le=200, description="Maximum number of results")
|
||||
offset: int = Field(0, ge=0, description="Pagination offset")
|
||||
sort: str = Field("relevance", description="Sort by 'relevance' or 'modified'")
|
||||
case_sensitive: bool = Field(False, description="Match case")
|
||||
whole_word: bool = Field(False, description="Match whole words only")
|
||||
regex: bool = Field(False, description="Treat query as a regular expression")
|
||||
include_paths: str | None = Field(None, description="Comma-separated glob patterns to include")
|
||||
exclude_paths: str | None = Field(None, description="Comma-separated glob patterns to exclude")
|
||||
created: str | None = Field(None, description="Created date filter (>date, <date, date..date)")
|
||||
modified: str | None = Field(None, description="Modified date filter (>date, <date, date..date, <Nd)")
|
||||
size: str | None = Field(None, description="Size filter (>size, <size, size..size, e.g. >1MB)")
|
||||
|
||||
|
||||
class SearchPathsInput(BaseModel):
|
||||
"""Search files and directories by path substring."""
|
||||
|
||||
q: str = Field(..., min_length=1, description="Path substring to search for")
|
||||
vault: str = Field("all", description="Vault name or 'all'")
|
||||
|
||||
|
||||
class SuggestTagsInput(BaseModel):
|
||||
"""Suggest tags matching a prefix."""
|
||||
|
||||
q: str = Field(..., min_length=1, description="Tag prefix (with or without leading '#')")
|
||||
vault: str = Field("all", description="Vault name or 'all'")
|
||||
limit: int = Field(10, ge=1, le=50, description="Maximum number of suggestions")
|
||||
|
||||
|
||||
class ListRecentInput(BaseModel):
|
||||
"""List recently opened (or modified) files."""
|
||||
|
||||
vault: str | None = Field(None, description="Optional single-vault filter")
|
||||
limit: int = Field(20, ge=1, le=200, description="Maximum number of files")
|
||||
mode: str = Field("opened", description="'opened' (history) or 'modified'")
|
||||
|
||||
|
||||
# ── D. Mutations ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class CreateFileInput(BaseModel):
|
||||
"""Create a new text file in a vault."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative path of the new file")
|
||||
content: str = Field("", description="Initial file content")
|
||||
|
||||
|
||||
class CreateDirectoryInput(BaseModel):
|
||||
"""Create a new directory in a vault."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative path of the new directory")
|
||||
|
||||
|
||||
class EditFileInput(BaseModel):
|
||||
"""Overwrite the full content of an existing file."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
content: str = Field(..., description="New full content of the file")
|
||||
|
||||
|
||||
class AppendToFileInput(BaseModel):
|
||||
"""Append text to the end of an existing file."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
content: str = Field(..., description="Text to append")
|
||||
|
||||
|
||||
class RenameFileInput(BaseModel):
|
||||
"""Rename a file in place (same parent directory)."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Current vault-relative file path")
|
||||
new_name: str = Field(..., description="New file name (no directory separator)")
|
||||
|
||||
|
||||
class RenameDirectoryInput(BaseModel):
|
||||
"""Rename a directory in place (same parent directory)."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Current vault-relative directory path")
|
||||
new_name: str = Field(..., description="New directory name (no directory separator)")
|
||||
|
||||
|
||||
class MovePathInput(BaseModel):
|
||||
"""Move a file or directory to another directory within the same vault."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
source_path: str = Field(..., description="Current vault-relative path of the file/directory")
|
||||
destination_dir: str = Field("", description="Target directory path (empty = vault root)")
|
||||
|
||||
|
||||
class ReplaceInFilesInput(BaseModel):
|
||||
"""Find and replace text across vault files (destructive, dry-run by default)."""
|
||||
|
||||
find: str = Field(..., min_length=1, description="Text or pattern to search for")
|
||||
replace: str = Field("", description="Replacement text")
|
||||
vault: str = Field("all", description="Vault name or 'all'")
|
||||
case_sensitive: bool = Field(False, description="Match case")
|
||||
whole_word: bool = Field(False, description="Match whole words only")
|
||||
regex: bool = Field(False, description="Treat 'find' as a regular expression")
|
||||
include_paths: str | None = Field(None, description="Comma-separated glob patterns to include")
|
||||
exclude_paths: str | None = Field(None, description="Comma-separated glob patterns to exclude")
|
||||
replace_all: bool = Field(False, description="Apply the replacement (otherwise only preview)")
|
||||
dry_run: bool | None = Field(
|
||||
None,
|
||||
description="Preview matches without writing. Defaults to the opposite of replace_all.",
|
||||
)
|
||||
|
||||
|
||||
class DeleteFileInput(BaseModel):
|
||||
"""Delete a file from a vault (destructive)."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
|
||||
|
||||
class DeleteDirectoryInput(BaseModel):
|
||||
"""Delete a directory from a vault (destructive)."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative directory path")
|
||||
recursive: bool = Field(True, description="Delete non-empty directories recursively")
|
||||
|
||||
|
||||
class RestoreBackupInput(BaseModel):
|
||||
"""Restore a file from one of its backup versions."""
|
||||
|
||||
vault: str = Field(..., description="Vault name")
|
||||
path: str = Field(..., description="Vault-relative file path")
|
||||
version: int = Field(..., description="Backup timestamp to restore")
|
||||
|
||||
|
||||
class WebSearchInput(BaseModel):
|
||||
"""Search the public web through the configured meta-search engine."""
|
||||
|
||||
query: str = Field(..., description="Search terms")
|
||||
max_results: int = Field(5, ge=1, le=10, description="Number of results to return")
|
||||
category: str = Field("", description="Optional engine category: general, news, it, science")
|
||||
language: str = Field("", description="Optional language code, e.g. 'fr'")
|
||||
page: int = Field(1, ge=1, le=10, description="Result page number")
|
||||
|
||||
|
||||
class FetchUrlInput(BaseModel):
|
||||
"""Fetch one public web page and return its readable text."""
|
||||
|
||||
url: str = Field(..., description="Absolute http(s) URL of a public page")
|
||||
|
||||
|
||||
class ToolResult(BaseModel):
|
||||
"""Uniform result returned by :func:`backend.tools.registry.call_tool`."""
|
||||
|
||||
ok: bool = True
|
||||
data: Any = None
|
||||
error: str | None = None
|
||||
@@ -0,0 +1,508 @@
|
||||
"""Built-in tool services (Phase 0 + Phase C read/search + Phase D mutations).
|
||||
|
||||
These functions are the single source of truth consumed by both the in-app
|
||||
assistant and the MCP server. They delegate to the shared business-logic
|
||||
services (``backend.services``) so routes and tools never diverge.
|
||||
Mutating tools (create/edit/rename/move/delete/restore) are registered with
|
||||
``ToolRisk.WRITE`` or ``ToolRisk.DANGEROUS`` and gated by the registry's
|
||||
confirmation mechanism (two-step propose/apply).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from backend.indexer import get_backlinks as _get_backlinks
|
||||
from backend.indexer import get_vault_names
|
||||
from backend.services.backups import diff_backup as _diff_backup
|
||||
from backend.services.backups import list_backup_files as _list_backup_files
|
||||
from backend.services.files import read_file_text
|
||||
from backend.services.graph import get_graph as _get_graph
|
||||
from backend.services.mutations import (
|
||||
append_to_file as _append_to_file,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
create_directory as _create_directory,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
create_file as _create_file,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
delete_directory as _delete_directory,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
delete_file as _delete_file,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
edit_file as _edit_file,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
move_path as _move_path,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
rename_directory as _rename_directory,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
rename_file as _rename_file,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
replace_in_files as _replace_in_files,
|
||||
)
|
||||
from backend.services.mutations import (
|
||||
restore_backup as _restore_backup,
|
||||
)
|
||||
from backend.services.recent import list_recent as _list_recent
|
||||
from backend.services.search import advanced_search_vaults, search_vaults
|
||||
from backend.services.search import list_tags as _list_tags
|
||||
from backend.services.search import search_paths as _search_paths
|
||||
from backend.services.vaults import browse_directory, list_accessible_vaults, list_all_files
|
||||
from backend.tools.context import ToolContext, ToolRisk
|
||||
from backend.tools.registry import tool
|
||||
from backend.tools.schemas import (
|
||||
AppendToFileInput,
|
||||
CreateDirectoryInput,
|
||||
CreateFileInput,
|
||||
DeleteDirectoryInput,
|
||||
DeleteFileInput,
|
||||
DiffBackupInput,
|
||||
EditFileInput,
|
||||
GetBacklinksInput,
|
||||
GetGraphInput,
|
||||
ListAllFilesInput,
|
||||
ListBackupsInput,
|
||||
ListDirectoryInput,
|
||||
ListRecentInput,
|
||||
ListTagsInput,
|
||||
ListVaultsInput,
|
||||
MovePathInput,
|
||||
ReadFileInput,
|
||||
ReadFileRawInput,
|
||||
RenameDirectoryInput,
|
||||
RenameFileInput,
|
||||
ReplaceInFilesInput,
|
||||
RestoreBackupInput,
|
||||
SearchAdvancedInput,
|
||||
SearchFulltextInput,
|
||||
SearchPathsInput,
|
||||
SuggestTagsInput,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("obsigate.tools.service")
|
||||
|
||||
# Maximum file size returned by ``read_file`` (bytes). Quota configurable via
|
||||
# ``BOOKSLM_MAX_TOOL_READ_BYTES``.
|
||||
TOOL_MAX_READ_BYTES = int(os.environ.get("BOOKSLM_MAX_TOOL_READ_BYTES", "200000"))
|
||||
|
||||
|
||||
# ── C1. Vaults / navigation ────────────────────────────────────────────────
|
||||
|
||||
|
||||
@tool(
|
||||
name="list_vaults",
|
||||
description="List the vaults the current user is allowed to access.",
|
||||
input_model=ListVaultsInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def list_vaults(ctx: ToolContext, _params: ListVaultsInput) -> list[dict[str, Any]]:
|
||||
"""Return accessible vaults with a file count."""
|
||||
return [
|
||||
{"name": v["name"], "file_count": v["file_count"]}
|
||||
for v in list_accessible_vaults(ctx.user)
|
||||
]
|
||||
|
||||
|
||||
@tool(
|
||||
name="list_directory",
|
||||
description="List files and subdirectories of a directory inside a vault.",
|
||||
input_model=ListDirectoryInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def list_directory(ctx: ToolContext, params: ListDirectoryInput) -> list[dict[str, Any]]:
|
||||
"""Return the entries of a vault directory (direct children only)."""
|
||||
data = browse_directory(params.vault, params.path)
|
||||
return [
|
||||
{"name": item["name"], "path": item["path"], "type": item["type"]}
|
||||
for item in data["items"]
|
||||
]
|
||||
|
||||
|
||||
@tool(
|
||||
name="list_all_files",
|
||||
description="List every file of a vault (optionally under a subdirectory), newest first.",
|
||||
input_model=ListAllFilesInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def list_all_files_tool(ctx: ToolContext, params: ListAllFilesInput) -> dict[str, Any]:
|
||||
"""Return a flat list of files with metadata."""
|
||||
return list_all_files(params.vault, dir=params.dir, limit=params.limit, recursive=params.recursive)
|
||||
|
||||
|
||||
# ── C2. Content reading ────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@tool(
|
||||
name="read_file",
|
||||
description="Read the text content of a file inside a vault (secrets redacted).",
|
||||
input_model=ReadFileInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def read_file(ctx: ToolContext, params: ReadFileInput) -> dict[str, Any]:
|
||||
"""Return the (redacted) content of a vault file."""
|
||||
return read_file_text(
|
||||
params.vault,
|
||||
params.path,
|
||||
redact=True,
|
||||
max_bytes=TOOL_MAX_READ_BYTES,
|
||||
)
|
||||
|
||||
|
||||
@tool(
|
||||
name="read_file_raw",
|
||||
description="Read a file's raw text content, without the size cap (secrets redacted).",
|
||||
input_model=ReadFileRawInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def read_file_raw(ctx: ToolContext, params: ReadFileRawInput) -> dict[str, Any]:
|
||||
"""Return the full (redacted) raw text of a vault file."""
|
||||
data = read_file_text(params.vault, params.path, redact=True, max_bytes=None)
|
||||
return {"vault": data["vault"], "path": data["path"], "raw": data["content"]}
|
||||
|
||||
|
||||
@tool(
|
||||
name="get_backlinks",
|
||||
description="List files that link to a target file via [[wikilinks]].",
|
||||
input_model=GetBacklinksInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def get_backlinks(ctx: ToolContext, params: GetBacklinksInput) -> list[dict[str, Any]]:
|
||||
"""Return backlinks, filtered to accessible vaults."""
|
||||
backlinks = _get_backlinks(params.vault, params.path)
|
||||
return [b for b in backlinks if ctx.has_vault_access(b["vault"])]
|
||||
|
||||
|
||||
@tool(
|
||||
name="list_backups",
|
||||
description="List the available backup versions of a file (newest first).",
|
||||
input_model=ListBackupsInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def list_backups(ctx: ToolContext, params: ListBackupsInput) -> dict[str, Any]:
|
||||
"""Return the backup versions of a vault file."""
|
||||
return {
|
||||
"vault": params.vault,
|
||||
"path": params.path,
|
||||
"backups": _list_backup_files(params.vault, params.path),
|
||||
}
|
||||
|
||||
|
||||
@tool(
|
||||
name="diff_backup",
|
||||
description="Show a unified diff between a backup version and another version or the current file.",
|
||||
input_model=DiffBackupInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def diff_backup(ctx: ToolContext, params: DiffBackupInput) -> dict[str, Any]:
|
||||
"""Return the unified diff for a backup version."""
|
||||
return _diff_backup(params.vault, params.path, params.version, params.compare_with)
|
||||
|
||||
|
||||
@tool(
|
||||
name="get_graph",
|
||||
description="Return the graph (nodes and edges: parent/child + wikilinks) of a vault or directory.",
|
||||
input_model=GetGraphInput,
|
||||
risk=ToolRisk.READ,
|
||||
requires_vault=True,
|
||||
)
|
||||
def get_graph(ctx: ToolContext, params: GetGraphInput) -> dict[str, Any]:
|
||||
"""Return graph data for a vault subtree or the whole vault."""
|
||||
return _get_graph(
|
||||
params.vault,
|
||||
path=params.path,
|
||||
depth=params.depth,
|
||||
scope=params.scope,
|
||||
tag=params.tag,
|
||||
)
|
||||
|
||||
|
||||
# ── C3. Search ─────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@tool(
|
||||
name="search_fulltext",
|
||||
description="Full-text search across one vault or all accessible vaults.",
|
||||
input_model=SearchFulltextInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def search_fulltext(ctx: ToolContext, params: SearchFulltextInput) -> list[dict[str, Any]]:
|
||||
"""Return ranked search results, filtered to accessible vaults."""
|
||||
payload = search_vaults(params.q, vault=params.vault, tag=params.tag, limit=params.limit)
|
||||
return [r for r in payload["results"] if ctx.has_vault_access(r["vault"])]
|
||||
|
||||
|
||||
@tool(
|
||||
name="search_advanced",
|
||||
description="Advanced full-text search with operators, filters and facets.",
|
||||
input_model=SearchAdvancedInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def search_advanced(ctx: ToolContext, params: SearchAdvancedInput) -> list[dict[str, Any]]:
|
||||
"""Return advanced search results, filtered to accessible vaults."""
|
||||
payload = advanced_search_vaults(
|
||||
params.q,
|
||||
vault=params.vault,
|
||||
tag=params.tag,
|
||||
limit=params.limit,
|
||||
offset=params.offset,
|
||||
sort=params.sort,
|
||||
case_sensitive=params.case_sensitive,
|
||||
whole_word=params.whole_word,
|
||||
regex=params.regex,
|
||||
include_paths=params.include_paths,
|
||||
exclude_paths=params.exclude_paths,
|
||||
created=params.created,
|
||||
modified=params.modified,
|
||||
size=params.size,
|
||||
)
|
||||
return [r for r in payload["results"] if ctx.has_vault_access(r["vault"])]
|
||||
|
||||
|
||||
@tool(
|
||||
name="search_paths",
|
||||
description="Search files and directories by path substring.",
|
||||
input_model=SearchPathsInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def search_paths(ctx: ToolContext, params: SearchPathsInput) -> list[dict[str, Any]]:
|
||||
"""Return path matches, filtered to accessible vaults."""
|
||||
payload = _search_paths(params.q, vault=params.vault)
|
||||
return [r for r in payload["results"] if ctx.has_vault_access(r["vault"])]
|
||||
|
||||
|
||||
@tool(
|
||||
name="list_tags",
|
||||
description="List tags with their occurrence counts for a vault or all accessible vaults.",
|
||||
input_model=ListTagsInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def list_tags(ctx: ToolContext, params: ListTagsInput) -> list[dict[str, Any]]:
|
||||
"""Return tags sorted by descending count."""
|
||||
if params.vault and params.vault != "all":
|
||||
ctx.require_vault_access(params.vault)
|
||||
return [{"tag": tag, "count": count} for tag, count in _list_tags(params.vault).items()]
|
||||
|
||||
merged: dict[str, int] = {}
|
||||
for name in get_vault_names():
|
||||
if not ctx.has_vault_access(name):
|
||||
continue
|
||||
for tag, count in _list_tags(name).items():
|
||||
merged[tag] = merged.get(tag, 0) + count
|
||||
return [{"tag": tag, "count": count} for tag, count in sorted(merged.items(), key=lambda x: -x[1])]
|
||||
|
||||
|
||||
@tool(
|
||||
name="suggest_tags",
|
||||
description="Suggest tags matching a prefix, across accessible vaults.",
|
||||
input_model=SuggestTagsInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def suggest_tags(ctx: ToolContext, params: SuggestTagsInput) -> list[dict[str, Any]]:
|
||||
"""Return tag suggestions, restricted to accessible vaults."""
|
||||
from backend.search import suggest_tags as _suggest
|
||||
|
||||
if params.vault and params.vault != "all":
|
||||
ctx.require_vault_access(params.vault)
|
||||
return _suggest(params.q, vault_filter=params.vault, limit=params.limit)
|
||||
|
||||
merged: dict[str, int] = {}
|
||||
for name in get_vault_names():
|
||||
if not ctx.has_vault_access(name):
|
||||
continue
|
||||
for item in _suggest(params.q, vault_filter=name, limit=params.limit):
|
||||
merged[item["tag"]] = merged.get(item["tag"], 0) + item["count"]
|
||||
ordered = sorted(merged.items(), key=lambda x: -x[1])
|
||||
return [{"tag": tag, "count": count} for tag, count in ordered[: params.limit]]
|
||||
|
||||
|
||||
@tool(
|
||||
name="list_recent",
|
||||
description="List the current user's recently opened (or modified) files.",
|
||||
input_model=ListRecentInput,
|
||||
risk=ToolRisk.READ,
|
||||
)
|
||||
def list_recent(ctx: ToolContext, params: ListRecentInput) -> dict[str, Any]:
|
||||
"""Return recent files for the calling user."""
|
||||
user_vaults = ctx.user.get("_token_vaults") or ctx.user.get("vaults", [])
|
||||
return _list_recent(
|
||||
ctx.username,
|
||||
user_vaults,
|
||||
vault=params.vault,
|
||||
limit=params.limit,
|
||||
mode=params.mode,
|
||||
)
|
||||
|
||||
|
||||
# ── D. Mutations ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@tool(
|
||||
name="create_file",
|
||||
description="Create a new text file in a vault with optional initial content.",
|
||||
input_model=CreateFileInput,
|
||||
risk=ToolRisk.WRITE,
|
||||
requires_vault=True,
|
||||
)
|
||||
def create_file(ctx: ToolContext, params: CreateFileInput) -> dict[str, Any]:
|
||||
"""Create a vault file (fails if it already exists)."""
|
||||
return _create_file(params.vault, params.path, params.content)
|
||||
|
||||
|
||||
@tool(
|
||||
name="create_directory",
|
||||
description="Create a new directory (and parents) in a vault.",
|
||||
input_model=CreateDirectoryInput,
|
||||
risk=ToolRisk.WRITE,
|
||||
requires_vault=True,
|
||||
)
|
||||
def create_directory(ctx: ToolContext, params: CreateDirectoryInput) -> dict[str, Any]:
|
||||
"""Create a vault directory."""
|
||||
return _create_directory(params.vault, params.path)
|
||||
|
||||
|
||||
@tool(
|
||||
name="edit_file",
|
||||
description="Overwrite the full content of an existing file (automatic backup).",
|
||||
input_model=EditFileInput,
|
||||
risk=ToolRisk.WRITE,
|
||||
requires_vault=True,
|
||||
)
|
||||
def edit_file(ctx: ToolContext, params: EditFileInput) -> dict[str, Any]:
|
||||
"""Replace a vault file's content."""
|
||||
return _edit_file(params.vault, params.path, params.content)
|
||||
|
||||
|
||||
@tool(
|
||||
name="append_to_file",
|
||||
description="Append text to the end of an existing file (automatic backup).",
|
||||
input_model=AppendToFileInput,
|
||||
risk=ToolRisk.WRITE,
|
||||
requires_vault=True,
|
||||
)
|
||||
def append_to_file(ctx: ToolContext, params: AppendToFileInput) -> dict[str, Any]:
|
||||
"""Append content to a vault file."""
|
||||
return _append_to_file(params.vault, params.path, params.content)
|
||||
|
||||
|
||||
@tool(
|
||||
name="rename_file",
|
||||
description="Rename a file in place (same parent directory). Destructive: requires confirmation.",
|
||||
input_model=RenameFileInput,
|
||||
risk=ToolRisk.DANGEROUS,
|
||||
requires_vault=True,
|
||||
)
|
||||
def rename_file(ctx: ToolContext, params: RenameFileInput) -> dict[str, Any]:
|
||||
"""Rename a vault file."""
|
||||
return _rename_file(params.vault, params.path, params.new_name)
|
||||
|
||||
|
||||
@tool(
|
||||
name="rename_directory",
|
||||
description="Rename a directory in place (same parent directory). Destructive: requires confirmation.",
|
||||
input_model=RenameDirectoryInput,
|
||||
risk=ToolRisk.DANGEROUS,
|
||||
requires_vault=True,
|
||||
)
|
||||
def rename_directory(ctx: ToolContext, params: RenameDirectoryInput) -> dict[str, Any]:
|
||||
"""Rename a vault directory."""
|
||||
return _rename_directory(params.vault, params.path, params.new_name)
|
||||
|
||||
|
||||
@tool(
|
||||
name="move_path",
|
||||
description="Move a file or directory to another directory in the same vault. Destructive: requires confirmation.",
|
||||
input_model=MovePathInput,
|
||||
risk=ToolRisk.DANGEROUS,
|
||||
requires_vault=True,
|
||||
)
|
||||
def move_path(ctx: ToolContext, params: MovePathInput) -> dict[str, Any]:
|
||||
"""Move a vault file or directory."""
|
||||
return _move_path(params.vault, params.source_path, params.destination_dir)
|
||||
|
||||
|
||||
@tool(
|
||||
name="replace_in_files",
|
||||
description=(
|
||||
"Find and replace text across vault files. Previews by default "
|
||||
"(dry_run); set replace_all=true to apply. Destructive: requires confirmation."
|
||||
),
|
||||
input_model=ReplaceInFilesInput,
|
||||
risk=ToolRisk.DANGEROUS,
|
||||
)
|
||||
def replace_in_files(ctx: ToolContext, params: ReplaceInFilesInput) -> dict[str, Any]:
|
||||
"""Preview or apply a find/replace, filtered to permitted vaults."""
|
||||
if params.vault and params.vault != "all":
|
||||
ctx.require_vault_access(params.vault)
|
||||
ctx.require_destructive_allowed(params.vault)
|
||||
|
||||
dry_run = params.dry_run if params.dry_run is not None else not params.replace_all
|
||||
|
||||
def _allowed(vault: str) -> bool:
|
||||
return ctx.has_vault_access(vault) and ctx.destructive_tools_enabled(vault)
|
||||
|
||||
return _replace_in_files(
|
||||
params.find,
|
||||
params.replace,
|
||||
vault=params.vault,
|
||||
case_sensitive=params.case_sensitive,
|
||||
whole_word=params.whole_word,
|
||||
regex=params.regex,
|
||||
include_paths=params.include_paths,
|
||||
exclude_paths=params.exclude_paths,
|
||||
replace_all=params.replace_all,
|
||||
dry_run=dry_run,
|
||||
is_vault_allowed=_allowed,
|
||||
)
|
||||
|
||||
|
||||
@tool(
|
||||
name="delete_file",
|
||||
description="Delete a file from a vault (automatic backup). Destructive: requires confirmation.",
|
||||
input_model=DeleteFileInput,
|
||||
risk=ToolRisk.DANGEROUS,
|
||||
requires_vault=True,
|
||||
)
|
||||
def delete_file(ctx: ToolContext, params: DeleteFileInput) -> dict[str, Any]:
|
||||
"""Delete a vault file."""
|
||||
return _delete_file(params.vault, params.path)
|
||||
|
||||
|
||||
@tool(
|
||||
name="delete_directory",
|
||||
description="Delete a directory (recursive by default) from a vault. Destructive: requires confirmation.",
|
||||
input_model=DeleteDirectoryInput,
|
||||
risk=ToolRisk.DANGEROUS,
|
||||
requires_vault=True,
|
||||
)
|
||||
def delete_directory(ctx: ToolContext, params: DeleteDirectoryInput) -> dict[str, Any]:
|
||||
"""Delete a vault directory."""
|
||||
return _delete_directory(params.vault, params.path, recursive=params.recursive)
|
||||
|
||||
|
||||
@tool(
|
||||
name="restore_backup",
|
||||
description="Restore a file from one of its backup versions (current version backed up first).",
|
||||
input_model=RestoreBackupInput,
|
||||
risk=ToolRisk.WRITE,
|
||||
requires_vault=True,
|
||||
)
|
||||
def restore_backup(ctx: ToolContext, params: RestoreBackupInput) -> dict[str, Any]:
|
||||
"""Restore a vault file from a backup."""
|
||||
return _restore_backup(params.vault, params.path, params.version)
|
||||
@@ -0,0 +1,234 @@
|
||||
"""Web tools for the assistant (Notion-style "research" capabilities).
|
||||
|
||||
Phase 1 of the documented web-toolset roadmap:
|
||||
|
||||
* ``web_search`` — query the self-hosted SearXNG instance (no API key).
|
||||
* ``fetch_url`` — retrieve a public web page and return readable text.
|
||||
|
||||
Both are READ-risk tools (no confirmation), rate-limited through the shared
|
||||
registry, SSRF-guarded (scheme + private-address rejection), and size-capped.
|
||||
|
||||
Configuration (environment):
|
||||
* ``OBSIGATE_SEARXNG_URL`` — defaults to https://search.dracodev.net
|
||||
* ``OBSIGATE_WEB_TIMEOUT`` — seconds, default 10
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import html as html_lib
|
||||
import ipaddress
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import httpx
|
||||
|
||||
from backend.tools.context import ToolError, ToolRisk, ToolScope
|
||||
from backend.tools.registry import tool
|
||||
from backend.tools.schemas import FetchUrlInput, WebSearchInput
|
||||
|
||||
logger = logging.getLogger("obsigate.tools.web")
|
||||
|
||||
SEARXNG_URL = os.environ.get("OBSIGATE_SEARXNG_URL", "https://search.dracodev.net")
|
||||
WEB_TIMEOUT = float(os.environ.get("OBSIGATE_WEB_TIMEOUT", "10"))
|
||||
USER_AGENT = "ObsiGateAssistant/1.0 (+self-hosted vault AI)"
|
||||
MAX_FETCH_BYTES = 1_500_000
|
||||
MAX_TEXT_CHARS = 20_000
|
||||
|
||||
_BLOCKED_TAGS_RE = re.compile(
|
||||
r"<(script|style|noscript|template|svg)\b.*?</\1>", re.IGNORECASE | re.DOTALL
|
||||
)
|
||||
_TAG_RE = re.compile(r"<[^>]+>")
|
||||
_BLOCK_SPLIT_RE = re.compile(
|
||||
r"</?(?:p|div|br|li|h[1-6]|tr|table|ul|ol|section|article|header|footer)\b[^>]*>",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
class SSRFError(ToolError):
|
||||
"""Raised for a URL whose host is private/loopback or scheme unsupported."""
|
||||
|
||||
|
||||
def _assert_public_http_url(url: str) -> str:
|
||||
"""Reject non-http(s) schemes and private/loopback/link-local targets."""
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
except ValueError as e:
|
||||
raise SSRFError("URL invalide", code="invalid_url") from e
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise SSRFError("Seuls les schémas http/https sont autorisés", code="invalid_scheme")
|
||||
host = parsed.hostname
|
||||
if not host:
|
||||
raise SSRFError("URL sans hôte", code="invalid_url")
|
||||
# Resolve the host so DNS-rebinding to internal IPs is also caught.
|
||||
try:
|
||||
infos = socket.getaddrinfo(host, None)
|
||||
except socket.gaierror as e:
|
||||
raise SSRFError(f"Hôte introuvable: {host}", code="dns_error") from e
|
||||
for info in infos:
|
||||
ip = ipaddress.ip_address(info[4][0])
|
||||
if (
|
||||
ip.is_private
|
||||
or ip.is_loopback
|
||||
or ip.is_link_local
|
||||
or ip.is_reserved
|
||||
or ip.is_multicast
|
||||
or ip.is_unspecified
|
||||
):
|
||||
raise SSRFError("Accès aux adresses internes interdit", code="ssrf_blocked")
|
||||
return url
|
||||
|
||||
|
||||
def _html_to_text(raw: str) -> str:
|
||||
"""Cheap HTML → readable text: strip scripts/styles, tags, then compress.
|
||||
|
||||
Comments (which may carry script-like payloads) are removed first.
|
||||
"""
|
||||
text = re.sub(r"<!--.*?-->", " ", raw, flags=re.DOTALL)
|
||||
text = _BLOCKED_TAGS_RE.sub(" ", text)
|
||||
# Keep block boundaries as newlines before dropping the remaining tags.
|
||||
text = _BLOCK_SPLIT_RE.sub("\n", text)
|
||||
text = _TAG_RE.sub("", text)
|
||||
text = html_lib.unescape(text)
|
||||
text = re.sub(r"[ \t]+", " ", text)
|
||||
text = re.sub(r" ?\n ?", "\n", text)
|
||||
text = re.sub(r"\n{3,}", "\n\n", text)
|
||||
return text.strip()
|
||||
|
||||
|
||||
@tool(
|
||||
name="web_search",
|
||||
description=(
|
||||
"Search the public web for current information and return ranked results "
|
||||
"(title, url, snippet). Use for facts outside the vault: weather, news, "
|
||||
"documentation, versions, prices, anything that needs live sources."
|
||||
),
|
||||
input_model=WebSearchInput,
|
||||
risk=ToolRisk.READ,
|
||||
scopes=(ToolScope.IN_APP,),
|
||||
)
|
||||
def web_search(ctx, params: WebSearchInput) -> dict[str, Any]:
|
||||
"""Query the self-hosted SearXNG instance and return trimmed results."""
|
||||
query = params.query.strip()
|
||||
if not query:
|
||||
raise ToolError("Requête vide", code="invalid_arguments")
|
||||
url = SEARXNG_URL.rstrip("/") + "/search"
|
||||
try:
|
||||
resp = httpx.get(
|
||||
url,
|
||||
params={
|
||||
"q": query,
|
||||
"format": "json",
|
||||
"categories": params.category or "general",
|
||||
"pageno": max(1, params.page),
|
||||
**({"language": params.language} if params.language else {}),
|
||||
"safesearch": "1",
|
||||
},
|
||||
headers={"User-Agent": USER_AGENT},
|
||||
timeout=WEB_TIMEOUT,
|
||||
follow_redirects=False,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
except httpx.HTTPError as e:
|
||||
logger.warning("web_search failed: %s", e)
|
||||
raise ToolError(
|
||||
"Le moteur de recherche web est momentanément indisponible.",
|
||||
code="web_search_unavailable",
|
||||
) from e
|
||||
results: list[dict[str, Any]] = []
|
||||
for item in (data.get("results") or [])[: params.max_results]:
|
||||
results.append(
|
||||
{
|
||||
"title": (item.get("title") or "")[:300],
|
||||
"url": item.get("url") or "",
|
||||
"snippet": (item.get("content") or "")[:600],
|
||||
"published": item.get("publishedDate"),
|
||||
"score": item.get("score"),
|
||||
}
|
||||
)
|
||||
unresponsive = [
|
||||
name for entry in (data.get("unresponsive_engines") or [])
|
||||
for name in ([entry[0]] if isinstance(entry, (list, tuple)) and entry else [entry])
|
||||
if isinstance(name, str)
|
||||
]
|
||||
payload: dict[str, Any] = {
|
||||
"query": query,
|
||||
"engine": "searxng",
|
||||
"results": results,
|
||||
"count": len(results),
|
||||
}
|
||||
if unresponsive:
|
||||
payload["unresponsive_engines"] = unresponsive[:8]
|
||||
if not results:
|
||||
# An instance whose upstream engines are all blocked (CAPTCHA / rate
|
||||
# limit) answers 200 with an empty list. Without an explicit hint the
|
||||
# model retries the same search until it burns its tool quota.
|
||||
payload["warning"] = (
|
||||
"Aucun résultat : les moteurs de recherche de l'instance SearXNG sont "
|
||||
f"indisponibles ({', '.join(unresponsive[:5]) or 'inconnus'}). "
|
||||
"Ne relance pas la même recherche — dis-le à l'utilisateur."
|
||||
)
|
||||
return payload
|
||||
|
||||
|
||||
@tool(
|
||||
name="fetch_url",
|
||||
description=(
|
||||
"Fetch a public web page (http/https) and return its readable text. "
|
||||
"Use after web_search to read a promising result in detail. HTML is "
|
||||
"converted to plain text; binary pages are rejected."
|
||||
),
|
||||
input_model=FetchUrlInput,
|
||||
risk=ToolRisk.READ,
|
||||
scopes=(ToolScope.IN_APP,),
|
||||
)
|
||||
def fetch_url(ctx, params: FetchUrlInput) -> dict[str, Any]:
|
||||
"""Retrieve one page, guard against SSRF, and extract its text."""
|
||||
url = _assert_public_http_url(params.url.strip())
|
||||
try:
|
||||
# Follow redirects manually so every hop is re-checked against the
|
||||
# private-address SSRF guard (a public page can redirect to 127.0.0.1).
|
||||
resp = None
|
||||
for _hop in range(5):
|
||||
resp = httpx.get(
|
||||
url,
|
||||
headers={"User-Agent": USER_AGENT, "Accept": "text/html,application/xhtml+xml,*/*"},
|
||||
timeout=WEB_TIMEOUT,
|
||||
follow_redirects=False,
|
||||
)
|
||||
if resp.status_code in (301, 302, 303, 307, 308):
|
||||
location = resp.headers.get("location") or ""
|
||||
if not location:
|
||||
break
|
||||
url = str(httpx.URL(url).join(location))
|
||||
url = _assert_public_http_url(url)
|
||||
continue
|
||||
break
|
||||
assert resp is not None
|
||||
resp.raise_for_status()
|
||||
except SSRFError:
|
||||
raise
|
||||
except httpx.HTTPError as e:
|
||||
logger.warning("fetch_url failed for %s: %s", url, e)
|
||||
raise ToolError("Impossible de récupérer la page.", code="fetch_unavailable") from e
|
||||
ctype = (resp.headers.get("content-type") or "").lower()
|
||||
if not any(t in ctype for t in ("html", "xml", "text", "json", "markdown")):
|
||||
raise ToolError(
|
||||
f"Type de contenu non pris en charge: {ctype.split(';')[0] or 'inconnu'}",
|
||||
code="unsupported_content_type",
|
||||
)
|
||||
raw = (resp.content[:MAX_FETCH_BYTES]).decode(resp.encoding or "utf-8", errors="replace")
|
||||
title_match = re.search(r"<title[^>]*>(.*?)</title>", raw, re.IGNORECASE | re.DOTALL)
|
||||
title = html_lib.unescape(title_match.group(1)).strip()[:300] if title_match else ""
|
||||
text = _html_to_text(raw)[:MAX_TEXT_CHARS]
|
||||
return {
|
||||
"url": str(resp.url),
|
||||
"status": resp.status_code,
|
||||
"title": title,
|
||||
"text": text,
|
||||
"truncated": len(raw) > MAX_TEXT_CHARS,
|
||||
}
|
||||
@@ -10,9 +10,9 @@ Current settings:
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger("obsigate.vault_settings")
|
||||
|
||||
@@ -21,10 +21,10 @@ _SETTINGS_PATH = _BASE_DIR / "data" / "vault_settings.json"
|
||||
_settings_lock = threading.RLock()
|
||||
|
||||
# In-memory cache of vault settings
|
||||
_vault_settings: Dict[str, Dict[str, Any]] = {}
|
||||
_vault_settings: dict[str, dict[str, Any]] = {}
|
||||
|
||||
|
||||
def load_vault_settings() -> Dict[str, Dict[str, Any]]:
|
||||
def load_vault_settings() -> dict[str, dict[str, Any]]:
|
||||
"""Load vault settings from disk.
|
||||
|
||||
Returns:
|
||||
@@ -78,7 +78,7 @@ def save_vault_settings() -> None:
|
||||
raise
|
||||
|
||||
|
||||
def get_vault_setting(vault_name: str) -> Optional[Dict[str, Any]]:
|
||||
def get_vault_setting(vault_name: str) -> dict[str, Any] | None:
|
||||
"""Get settings for a specific vault.
|
||||
|
||||
Args:
|
||||
@@ -91,7 +91,7 @@ def get_vault_setting(vault_name: str) -> Optional[Dict[str, Any]]:
|
||||
return _vault_settings.get(vault_name)
|
||||
|
||||
|
||||
def update_vault_setting(vault_name: str, settings: Dict[str, Any]) -> Dict[str, Any]:
|
||||
def update_vault_setting(vault_name: str, settings: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Update settings for a specific vault.
|
||||
|
||||
Args:
|
||||
@@ -128,7 +128,7 @@ def delete_vault_setting(vault_name: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def get_all_vault_settings() -> Dict[str, Dict[str, Any]]:
|
||||
def get_all_vault_settings() -> dict[str, dict[str, Any]]:
|
||||
"""Get all vault settings.
|
||||
|
||||
Returns:
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
"""
|
||||
Version management for ObsiGate.
|
||||
|
||||
Source unique de vérité : le fichier `VERSION` à la racine du dépôt, au format
|
||||
`MAJEUR.MINEUR.CORRECTIF` (incrémenté à chaque livraison par
|
||||
`scripts/bump_version.py`, hook git `commit-msg`). get_version() retourne
|
||||
toujours une chaîne propre "x.y.z" — jamais "-dev", "0.0.0-dev" ou "-N-gHASH" :
|
||||
le même numéro s'affiche dans le badge d'en-tête, la boîte À propos et /api/health.
|
||||
|
||||
Les sources sont consultées dans cet ordre :
|
||||
1. variable d'environnement OBSIGATE_VERSION (surcharge explicite / tests) ;
|
||||
2. `VERSION` à la racine du dépôt (source unique, copiée dans l'image Docker) ;
|
||||
3. `backend/VERSION` (ancien emplacement, encore produit par certains builds) ;
|
||||
4. dernier tag git (`git describe --tags --abbrev=0`) ;
|
||||
5. "0.0.0".
|
||||
|
||||
Exemples :
|
||||
VERSION = 2.3.0 -> "2.3.0"
|
||||
tag v2.3.0, 4 commits -> "2.3.0"
|
||||
aucun VERSION / git -> "0.0.0"
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
_ROOT = Path(__file__).resolve().parent.parent # racine du dépôt ObsiGate
|
||||
VERSION_FILE = _ROOT / "VERSION" # source unique de vérité
|
||||
LEGACY_VERSION_FILE = Path(__file__).resolve().parent / "VERSION" # backend/VERSION
|
||||
_ENV_VAR = "OBSIGATE_VERSION"
|
||||
|
||||
|
||||
def _run_git(args: list[str]) -> str:
|
||||
"""Run a git command in the repo root; return stdout (stripped) or ''."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", *args],
|
||||
cwd=str(_ROOT),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
check=False,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
return result.stdout.strip()
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
|
||||
pass
|
||||
return ""
|
||||
|
||||
|
||||
def _clean_base(raw: str) -> str:
|
||||
"""Reduce a version string to its numeric MAJOR.MINOR.PATCH base.
|
||||
|
||||
Handles "v2.0.0", "2.0.0-31-gabc1234", "2.0.0-dev", "0.0.0-dev".
|
||||
Returns "" if no numeric triplet can be parsed.
|
||||
"""
|
||||
s = (raw or "").strip().lstrip("vV")
|
||||
base = s.split("-")[0] # strip -N-gHASH / -dev / -dirty suffixes
|
||||
parts = base.split(".")
|
||||
nums = []
|
||||
for p in parts[:3]:
|
||||
if not p.isdigit():
|
||||
break
|
||||
nums.append(str(int(p)))
|
||||
if len(nums) != 3:
|
||||
return ""
|
||||
return ".".join(nums)
|
||||
|
||||
|
||||
def _read_version_file(path: Path) -> str:
|
||||
"""Clean base of a VERSION file, or '' when absent/unreadable/invalid."""
|
||||
try:
|
||||
if not path.exists():
|
||||
return ""
|
||||
return _clean_base(path.read_text(encoding="utf-8", errors="replace"))
|
||||
except OSError:
|
||||
return ""
|
||||
|
||||
|
||||
def get_git_describe() -> str:
|
||||
"""Full `git describe` string (e.g. "2.0.0-31-gabc1234") or '' if no git."""
|
||||
return _run_git(["describe", "--tags", "--dirty=-dirty"]).lstrip("v")
|
||||
|
||||
|
||||
def get_git_commit() -> str:
|
||||
"""Short HEAD commit hash (e.g. "abc1234") or '' if unavailable."""
|
||||
return _run_git(["rev-parse", "--short", "HEAD"])
|
||||
|
||||
|
||||
def get_version() -> str:
|
||||
"""Return the clean release version x.y.z — never a -suffix.
|
||||
|
||||
Priority: OBSIGATE_VERSION -> ./VERSION -> backend/VERSION -> latest git tag
|
||||
-> "0.0.0".
|
||||
"""
|
||||
# 1) Explicit override (docker-compose, tests, builds hors dépôt)
|
||||
override = _clean_base(os.environ.get(_ENV_VAR, ""))
|
||||
if override:
|
||||
return override
|
||||
|
||||
# 2) Source unique de vérité : VERSION à la racine du dépôt
|
||||
base = _read_version_file(VERSION_FILE)
|
||||
if base:
|
||||
return base
|
||||
|
||||
# 3) Ancien emplacement (backend/VERSION, baké par certains builds)
|
||||
base = _read_version_file(LEGACY_VERSION_FILE)
|
||||
if base:
|
||||
return base
|
||||
|
||||
# 4) Dernier tag git
|
||||
base = _clean_base(_run_git(["describe", "--tags", "--abbrev=0"]))
|
||||
if base:
|
||||
return base
|
||||
|
||||
# 5) Rien d'exploitable
|
||||
return "0.0.0"
|
||||
|
||||
|
||||
def get_version_tuple() -> tuple[int, int, int]:
|
||||
"""Return (major, minor, patch) tuple for programmatic use."""
|
||||
raw = get_version()
|
||||
base = _clean_base(raw) or "0.0.0"
|
||||
major, minor, patch = (int(p) for p in base.split("."))
|
||||
return (major, minor, patch)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(get_version())
|
||||
+69
-23
@@ -2,30 +2,64 @@ import asyncio
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from collections.abc import Callable, Iterable
|
||||
from pathlib import Path
|
||||
from typing import Callable, Dict, List, Optional
|
||||
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.observers.polling import PollingObserver
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from backend.indexer import SUPPORTED_EXTENSIONS
|
||||
|
||||
from backend.indexer import IGNORED_DIRS, SUPPORTED_EXTENSIONS
|
||||
|
||||
logger = logging.getLogger("obsigate.watcher")
|
||||
|
||||
# Extensions de fichiers surveillées
|
||||
# Default ignored directories (can be overridden via OBSIGATE_IGNORED_DIRS env var)
|
||||
_DEFAULT_IGNORED_DIRS = {'.obsidian', '.trash', '.git', '__pycache__', 'node_modules', '.obsigate-backup'}
|
||||
# Filesystem types where inotify does NOT fire for changes made by other
|
||||
# clients (the edit happens on another machine — e.g. Obsidian on Windows
|
||||
# writing to an NFS/SMB export). For those mounts watchdog's native
|
||||
# Observer silently misses everything and polling is the only option.
|
||||
NETWORK_FSTYPES = {
|
||||
"nfs", "nfs4", "cifs", "smbfs", "smb2", "smb3",
|
||||
"fuse", "fuse.sshfs", "glusterfs", "9p", "virtiofs", "lustre",
|
||||
}
|
||||
|
||||
def _load_ignored_dirs() -> set:
|
||||
"""Load ignored directories from environment or use defaults."""
|
||||
env_val = os.environ.get("OBSIGATE_IGNORED_DIRS", "")
|
||||
if env_val:
|
||||
custom = set(d.strip() for d in env_val.split(",") if d.strip())
|
||||
logger.info(f"Using custom IGNORED_DIRS: {custom}")
|
||||
return custom
|
||||
return _DEFAULT_IGNORED_DIRS.copy()
|
||||
|
||||
IGNORED_DIRS = _load_ignored_dirs()
|
||||
def find_mount_fstype(resolved_path: str, mount_lines: Iterable[str]) -> str:
|
||||
"""Return the fstype of the longest /proc/mounts entry covering the path.
|
||||
|
||||
Pure function (mount_lines are text lines) so it can be unit-tested
|
||||
without /proc. Returns "" when no mount matches.
|
||||
"""
|
||||
best_point = ""
|
||||
best_fstype = ""
|
||||
for line in mount_lines:
|
||||
parts = line.split()
|
||||
if len(parts) < 3:
|
||||
continue
|
||||
mount_point, fstype = parts[1], parts[2]
|
||||
# /proc/mounts escapes special chars in octal
|
||||
mount_point = mount_point.replace("\\040", " ").replace("\\011", "\t")
|
||||
root = mount_point.rstrip("/") or "/"
|
||||
if (resolved_path == root or resolved_path.startswith(root + "/")) and len(mount_point) > len(best_point):
|
||||
best_point = mount_point
|
||||
best_fstype = fstype
|
||||
return best_fstype
|
||||
|
||||
|
||||
def is_network_mount(path: str) -> bool:
|
||||
"""True if *path* sits on a network/fuse filesystem (Linux only).
|
||||
|
||||
inotify events are local to the client that made the write, so vaults
|
||||
shared over NFS/SMB never see Obsidian's edits. On those mounts we
|
||||
must fall back to PollingObserver, which detects changes by stat().
|
||||
"""
|
||||
if os.name != "posix":
|
||||
return False
|
||||
try:
|
||||
with open("/proc/mounts", "r", encoding="utf-8") as fh:
|
||||
fstype = find_mount_fstype(os.path.realpath(path), fh)
|
||||
except OSError:
|
||||
return False
|
||||
return fstype in NETWORK_FSTYPES
|
||||
|
||||
|
||||
class VaultEventHandler(FileSystemEventHandler):
|
||||
@@ -105,10 +139,10 @@ class VaultWatcher:
|
||||
self.polling_interval = polling_interval
|
||||
self.observers: dict[str, object] = {}
|
||||
self.event_queue: asyncio.Queue = asyncio.Queue()
|
||||
self._processor_task: Optional[asyncio.Task] = None
|
||||
self._processor_task: asyncio.Task | None = None
|
||||
self._running = False
|
||||
|
||||
async def start(self, vaults: Dict[str, str]):
|
||||
async def start(self, vaults: dict[str, str]):
|
||||
"""Démarrer la surveillance de toutes les vaults.
|
||||
|
||||
Args:
|
||||
@@ -129,7 +163,12 @@ class VaultWatcher:
|
||||
vault_path: str,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
):
|
||||
"""Créer et démarrer un observer pour une vault."""
|
||||
"""Créer et démarrer un observer pour une vault.
|
||||
|
||||
Le mode (natif inotify vs polling) est choisi par vault :
|
||||
les mounts réseau (NFS/SMB/fuse) sont surveillés en polling car
|
||||
inotify ne voit pas les écritures faites depuis d'autres clients.
|
||||
"""
|
||||
path = Path(vault_path)
|
||||
if not path.exists():
|
||||
logger.warning(f"Vault '{vault_name}' path not found: {vault_path}")
|
||||
@@ -137,16 +176,23 @@ class VaultWatcher:
|
||||
|
||||
handler = VaultEventHandler(vault_name, self.event_queue, loop)
|
||||
|
||||
ObserverClass = PollingObserver if self.use_polling else Observer
|
||||
polling = self.use_polling or is_network_mount(vault_path)
|
||||
if polling and not self.use_polling:
|
||||
logger.info(
|
||||
f"Vault '{vault_name}' is on a network mount — "
|
||||
"using polling watcher (inotify cannot see remote edits)"
|
||||
)
|
||||
|
||||
ObserverClass = PollingObserver if polling else Observer
|
||||
try:
|
||||
observer = ObserverClass(
|
||||
timeout=self.polling_interval if self.use_polling else 1
|
||||
timeout=self.polling_interval if polling else 1
|
||||
)
|
||||
observer.schedule(handler, str(path), recursive=True)
|
||||
observer.daemon = True
|
||||
observer.start()
|
||||
self.observers[vault_name] = observer
|
||||
mode = "polling" if self.use_polling else "native"
|
||||
mode = "polling" if polling else "native"
|
||||
logger.info(f"Watching ({mode}): {vault_name} -> {vault_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start watcher for '{vault_name}': {e}")
|
||||
@@ -186,7 +232,7 @@ class VaultWatcher:
|
||||
Accumule les événements pendant debounce_seconds puis appelle
|
||||
le callback une seule fois avec la liste consolidée.
|
||||
"""
|
||||
pending: Dict[str, dict] = {}
|
||||
pending: dict[str, dict] = {}
|
||||
|
||||
while self._running:
|
||||
try:
|
||||
@@ -210,7 +256,7 @@ class VaultWatcher:
|
||||
except Exception as e:
|
||||
logger.error(f"Event processor error: {e}")
|
||||
|
||||
async def _dispatch(self, events: List[dict]):
|
||||
async def _dispatch(self, events: list[dict]):
|
||||
"""Appeler le callback avec les événements consolidés."""
|
||||
try:
|
||||
await self.on_file_change(events)
|
||||
|
||||
+178
-22
@@ -4,26 +4,39 @@ Webhook management and dispatch for ObsiGate.
|
||||
Webhooks are HTTP POST callbacks triggered on file/directory events.
|
||||
Configuration is persisted in data/webhooks.json.
|
||||
|
||||
Security (BUG-026):
|
||||
* target URLs are validated against SSRF (scheme + resolved IP must be public);
|
||||
* HTTP is refused unless ``OBSIGATE_WEBHOOK_ALLOW_HTTP=true``;
|
||||
* private/loopback/link-local targets are refused unless
|
||||
``OBSIGATE_WEBHOOK_ALLOW_PRIVATE=true``;
|
||||
* redirects are never followed;
|
||||
* signing secrets are **not** stored in the public config file — they live in
|
||||
``data/webhook_secrets.json`` (0600) or in an environment variable named
|
||||
``OBSIGATE_WEBHOOK_SECRET_<ID>``.
|
||||
|
||||
Events: file_created, file_deleted, file_modified, file_renamed,
|
||||
directory_created, directory_deleted, directory_renamed
|
||||
"""
|
||||
|
||||
import json
|
||||
import hmac
|
||||
import hashlib
|
||||
import asyncio
|
||||
import hashlib
|
||||
import hmac
|
||||
import ipaddress
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional, List
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import aiohttp
|
||||
|
||||
|
||||
logger = logging.getLogger("obsigate.webhooks")
|
||||
|
||||
WEBHOOKS_FILE = Path("data/webhooks.json")
|
||||
WEBHOOK_SECRETS_FILE = Path("data/webhook_secrets.json")
|
||||
|
||||
VALID_EVENTS = {
|
||||
"file_created", "file_deleted", "file_modified", "file_renamed",
|
||||
@@ -31,6 +44,81 @@ VALID_EVENTS = {
|
||||
}
|
||||
|
||||
|
||||
def _allow_http() -> bool:
|
||||
return os.environ.get("OBSIGATE_WEBHOOK_ALLOW_HTTP", "false").lower() == "true"
|
||||
|
||||
|
||||
def _allow_private() -> bool:
|
||||
return os.environ.get("OBSIGATE_WEBHOOK_ALLOW_PRIVATE", "false").lower() == "true"
|
||||
|
||||
|
||||
def _is_public_ip(ip_str: str) -> bool:
|
||||
"""True when *ip_str* is a globally routable unicast address."""
|
||||
try:
|
||||
ip = ipaddress.ip_address(ip_str)
|
||||
except ValueError:
|
||||
return False
|
||||
return not (
|
||||
ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved
|
||||
or ip.is_multicast or ip.is_unspecified
|
||||
)
|
||||
|
||||
|
||||
def validate_webhook_url(url: str) -> str:
|
||||
"""Validate the URL syntax/scheme and literal-IP safety at config time.
|
||||
|
||||
Raises:
|
||||
ValueError: When the URL is malformed or points at an obviously
|
||||
forbidden scheme/host.
|
||||
"""
|
||||
if not url or not isinstance(url, str):
|
||||
raise ValueError("URL requise")
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValueError("L'URL doit utiliser http ou https")
|
||||
if parsed.scheme == "http" and not _allow_http():
|
||||
raise ValueError("HTTPS requis (définir OBSIGATE_WEBHOOK_ALLOW_HTTP=true pour autoriser http)")
|
||||
host = parsed.hostname
|
||||
if not host:
|
||||
raise ValueError("Hôte manquant dans l'URL")
|
||||
# Reject literal private/loopback IPs immediately (no DNS needed).
|
||||
try:
|
||||
ip = ipaddress.ip_address(host)
|
||||
except ValueError:
|
||||
return url # hostname — resolved and checked at dispatch time
|
||||
if not _allow_private() and not _is_public_ip(str(ip)):
|
||||
raise ValueError("Adresse privée/interne refusée")
|
||||
return url
|
||||
|
||||
|
||||
def is_safe_target(url: str) -> bool:
|
||||
"""Full SSRF check performed right before dispatch (resolves the host).
|
||||
|
||||
Returns False when the URL is malformed, the scheme is forbidden, or any
|
||||
resolved address is private/loopback/reserved.
|
||||
"""
|
||||
try:
|
||||
validate_webhook_url(url)
|
||||
except ValueError:
|
||||
return False
|
||||
if _allow_private():
|
||||
return True
|
||||
parsed = urlparse(url)
|
||||
host = parsed.hostname or ""
|
||||
port = parsed.port or (443 if parsed.scheme == "https" else 80)
|
||||
try:
|
||||
infos = socket.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)
|
||||
except socket.gaierror:
|
||||
logger.warning(f"Webhook target host could not be resolved: {host}")
|
||||
return False
|
||||
for info in infos:
|
||||
addr = str(info[4][0])
|
||||
if not _is_public_ip(addr):
|
||||
logger.warning(f"Webhook target resolves to a non-public address ({addr}); blocked")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _read() -> list:
|
||||
if not WEBHOOKS_FILE.exists():
|
||||
return []
|
||||
@@ -47,35 +135,94 @@ def _write(webhooks: list):
|
||||
tmp.replace(WEBHOOKS_FILE)
|
||||
|
||||
|
||||
def _read_secrets() -> dict:
|
||||
if not WEBHOOK_SECRETS_FILE.exists():
|
||||
return {}
|
||||
try:
|
||||
return json.loads(WEBHOOK_SECRETS_FILE.read_text(encoding="utf-8"))
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return {}
|
||||
|
||||
|
||||
def _write_secrets(secrets: dict):
|
||||
WEBHOOK_SECRETS_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = WEBHOOK_SECRETS_FILE.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(secrets, indent=2), encoding="utf-8")
|
||||
tmp.replace(WEBHOOK_SECRETS_FILE)
|
||||
try:
|
||||
WEBHOOK_SECRETS_FILE.chmod(0o600)
|
||||
except OSError:
|
||||
pass # Windows doesn't support Unix permissions
|
||||
|
||||
|
||||
def _store_secret(wh_id: str, secret: str | None) -> None:
|
||||
secrets = _read_secrets()
|
||||
if secret:
|
||||
secrets[wh_id] = secret
|
||||
else:
|
||||
secrets.pop(wh_id, None)
|
||||
_write_secrets(secrets)
|
||||
|
||||
|
||||
def _get_secret(wh: dict) -> str | None:
|
||||
"""Resolve a webhook secret from env, dedicated store, or legacy record."""
|
||||
env_key = "OBSIGATE_WEBHOOK_SECRET_" + wh["id"].replace("-", "_").upper()
|
||||
env_val = os.environ.get(env_key)
|
||||
if env_val:
|
||||
return env_val
|
||||
stored = _read_secrets().get(wh["id"])
|
||||
if stored:
|
||||
return stored
|
||||
return wh.get("secret") # legacy inline secret
|
||||
|
||||
|
||||
def _public_view(wh: dict) -> dict:
|
||||
"""Return a webhook record safe to expose through the API."""
|
||||
clean = {k: v for k, v in wh.items() if k != "secret"}
|
||||
clean["has_secret"] = bool(_get_secret(wh))
|
||||
return clean
|
||||
|
||||
|
||||
def get_webhooks() -> list:
|
||||
return _read()
|
||||
return [_public_view(wh) for wh in _read()]
|
||||
|
||||
|
||||
def create_webhook(name: str, url: str, events: List[str], secret: Optional[str] = None) -> dict:
|
||||
def create_webhook(name: str, url: str, events: list[str], secret: str | None = None) -> dict:
|
||||
validate_webhook_url(url)
|
||||
webhooks = _read()
|
||||
wh_id = str(uuid.uuid4())
|
||||
wh = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"id": wh_id,
|
||||
"name": name,
|
||||
"url": url,
|
||||
"events": [e for e in events if e in VALID_EVENTS],
|
||||
"secret": secret,
|
||||
"enabled": True,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"last_fired_at": None,
|
||||
}
|
||||
webhooks.append(wh)
|
||||
_write(webhooks)
|
||||
if secret:
|
||||
_store_secret(wh_id, secret)
|
||||
logger.info(f"Created webhook '{name}' → {url}")
|
||||
return wh
|
||||
return _public_view(wh)
|
||||
|
||||
|
||||
def update_webhook(wh_id: str, updates: dict) -> Optional[dict]:
|
||||
def update_webhook(wh_id: str, updates: dict) -> dict | None:
|
||||
webhooks = _read()
|
||||
for wh in webhooks:
|
||||
if wh["id"] == wh_id:
|
||||
wh.update({k: v for k, v in updates.items() if k != "id"})
|
||||
if updates.get("url"):
|
||||
validate_webhook_url(updates["url"])
|
||||
if "secret" in updates:
|
||||
_store_secret(wh_id, updates["secret"])
|
||||
safe_updates = {
|
||||
k: v for k, v in updates.items()
|
||||
if k not in ("id", "secret")
|
||||
}
|
||||
wh.update(safe_updates)
|
||||
_write(webhooks)
|
||||
return wh
|
||||
return _public_view(wh)
|
||||
return None
|
||||
|
||||
|
||||
@@ -85,6 +232,9 @@ def delete_webhook(wh_id: str) -> bool:
|
||||
if len(new_list) == len(webhooks):
|
||||
return False
|
||||
_write(new_list)
|
||||
secrets = _read_secrets()
|
||||
if secrets.pop(wh_id, None) is not None:
|
||||
_write_secrets(secrets)
|
||||
return True
|
||||
|
||||
|
||||
@@ -104,18 +254,24 @@ async def dispatch_webhooks(event_type: str, data: dict):
|
||||
|
||||
async def _post(wh):
|
||||
try:
|
||||
# BUG-026: re-check the target right before connecting (DNS rebinding).
|
||||
if not is_safe_target(wh["url"]):
|
||||
logger.warning(f"Webhook '{wh['name']}' blocked by SSRF policy")
|
||||
return
|
||||
headers = {"Content-Type": "application/json", "X-ObsiGate-Event": event_type}
|
||||
if wh.get("secret"):
|
||||
sig = hmac.new(wh["secret"].encode(), body.encode(), hashlib.sha256).hexdigest()
|
||||
secret = _get_secret(wh)
|
||||
if secret:
|
||||
sig = hmac.new(secret.encode(), body.encode(), hashlib.sha256).hexdigest()
|
||||
headers["X-ObsiGate-Signature"] = f"sha256={sig}"
|
||||
|
||||
timeout = aiohttp.ClientTimeout(total=5)
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
async with session.post(wh["url"], data=body, headers=headers) as resp:
|
||||
if resp.status < 400:
|
||||
logger.debug(f"Webhook '{wh['name']}' OK ({resp.status})")
|
||||
else:
|
||||
logger.warning(f"Webhook '{wh['name']}' failed ({resp.status})")
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session, session.post(
|
||||
wh["url"], data=body, headers=headers, allow_redirects=False
|
||||
) as resp:
|
||||
if resp.status < 400:
|
||||
logger.debug(f"Webhook '{wh['name']}' OK ({resp.status})")
|
||||
else:
|
||||
logger.warning(f"Webhook '{wh['name']}' failed ({resp.status})")
|
||||
update_webhook(wh["id"], {"last_fired_at": datetime.now(timezone.utc).isoformat()})
|
||||
except Exception as e:
|
||||
logger.warning(f"Webhook '{wh['name']}' error: {e}")
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
# =============================================================================
|
||||
# ObsiGate — Script de build et déploiement Docker (Windows PowerShell)
|
||||
# =============================================================================
|
||||
#
|
||||
# Usage :
|
||||
# .\build.ps1 [-Prod | -Test] [OPTIONS]
|
||||
#
|
||||
# Environnements :
|
||||
# -Prod Production (défaut) — docker-compose.yml
|
||||
# -Test Test Windows (Docker Desktop) — docker-compose.test-win.yml
|
||||
#
|
||||
# Options :
|
||||
# -BuildOnly Construit l'image sans démarrer le conteneur
|
||||
# -NoCache Rebuild complet sans cache Docker (défaut)
|
||||
# -UseCache Utilise le cache Docker (plus rapide si peu de changements)
|
||||
# -Help Affiche cette aide
|
||||
#
|
||||
# Exemples :
|
||||
# .\build.ps1 # Production
|
||||
# .\build.ps1 -Test # Test Windows
|
||||
# .\build.ps1 -Test -BuildOnly # Build test sans démarrer
|
||||
# .\build.ps1 -Prod -UseCache # Production avec cache
|
||||
# =============================================================================
|
||||
|
||||
param(
|
||||
[switch]$Prod,
|
||||
[switch]$Test,
|
||||
[switch]$BuildOnly,
|
||||
[switch]$NoCache,
|
||||
[switch]$UseCache,
|
||||
[switch]$Help
|
||||
)
|
||||
|
||||
# ----- Helpers -----
|
||||
function Write-Info { Write-Host "[INFO] $args" -ForegroundColor Cyan }
|
||||
function Write-Ok { Write-Host "[OK] $args" -ForegroundColor Green }
|
||||
function Write-Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
|
||||
function Write-Error { Write-Host "[ERR] $args" -ForegroundColor Red }
|
||||
|
||||
# ----- Help -----
|
||||
if ($Help) {
|
||||
Get-Help $PSCommandPath -Detailed
|
||||
exit 0
|
||||
}
|
||||
|
||||
# ----- Select environment -----
|
||||
$EnvMode = "prod"
|
||||
$ComposeEnvName = "production"
|
||||
if ($Test) {
|
||||
$EnvMode = "test-win"
|
||||
$ComposeEnvName = "test Windows"
|
||||
}
|
||||
|
||||
switch ($EnvMode) {
|
||||
"prod" { $ComposeFile = "docker-compose.yml" }
|
||||
"test-win" { $ComposeFile = "docker-compose.test-win.yml" }
|
||||
}
|
||||
|
||||
# ----- Pre-flight checks -----
|
||||
$dockerVersion = docker --version 2>$null
|
||||
if (-not $dockerVersion) {
|
||||
Write-Error "Docker est introuvable. Installez Docker Desktop avant de continuer."
|
||||
exit 1
|
||||
}
|
||||
Write-Ok "Docker détecté"
|
||||
|
||||
$composeVersion = docker compose version 2>$null
|
||||
if (-not $composeVersion) {
|
||||
Write-Error "Docker Compose est introuvable (plugin docker compose requis)."
|
||||
exit 1
|
||||
}
|
||||
Write-Ok $composeVersion.Trim()
|
||||
|
||||
# ----- Validate compose file -----
|
||||
if (-not (Test-Path $ComposeFile)) {
|
||||
Write-Error "$ComposeFile introuvable dans $(Get-Location)"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Info "=== ObsiGate — Build & Deploy ($ComposeEnvName) ==="
|
||||
Write-Info "Fichier compose : $ComposeFile"
|
||||
|
||||
# ----- Test mode: create test directories -----
|
||||
if ($EnvMode -eq "test-win") {
|
||||
Write-Info "Création des dossiers de test..."
|
||||
|
||||
$testVault = "C:\dev\git\python\ObsiGate\test_vault"
|
||||
$testDir = "C:\dev\git\python\ObsiGate\test_dir"
|
||||
|
||||
if (-not (Test-Path $testVault)) {
|
||||
New-Item -ItemType Directory -Force -Path $testVault | Out-Null
|
||||
}
|
||||
if (-not (Test-Path $testDir)) {
|
||||
New-Item -ItemType Directory -Force -Path $testDir | Out-Null
|
||||
}
|
||||
|
||||
$welcomeFile = Join-Path $testVault "Accueil.md"
|
||||
if (-not (Test-Path $welcomeFile)) {
|
||||
@"
|
||||
# Bienvenue dans le vault de test
|
||||
|
||||
Ce vault est utilisé pour les tests de développement d'ObsiGate.
|
||||
"@ | Out-File -Encoding UTF8 -FilePath $welcomeFile
|
||||
Write-Ok "Note de test créée : test_vault\Accueil.md"
|
||||
}
|
||||
}
|
||||
|
||||
# ----- Resolve version from git before Docker build -----
|
||||
$Version = (git describe --tags --dirty 2>$null) -replace '^v',''
|
||||
if (-not $Version) { $Version = "0.0.0" }
|
||||
$Version | Out-File -Encoding ascii -NoNewline -FilePath "backend\VERSION"
|
||||
# Export so docker compose substitutes ${VERSION} in the build arg
|
||||
$env:VERSION = $Version
|
||||
Write-Info "Version : $Version"
|
||||
|
||||
# ----- Build the image -----
|
||||
$BuildArgs = @("-f", $ComposeFile)
|
||||
if (-not $UseCache) {
|
||||
$BuildArgs += "--no-cache"
|
||||
Write-Info "Construction de l'image Docker (sans cache)..."
|
||||
} else {
|
||||
Write-Info "Construction de l'image Docker (avec cache)..."
|
||||
}
|
||||
|
||||
docker compose @BuildArgs build
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Error "Échec de la construction de l'image Docker."
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
Write-Ok "Image construite avec succès"
|
||||
|
||||
# ----- Stop if build-only -----
|
||||
if ($BuildOnly) {
|
||||
Write-Host ""
|
||||
Write-Info "Mode build-only : conteneur non démarré."
|
||||
Write-Info "Pour démarrer manuellement : docker compose -f $ComposeFile up -d"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# ----- Start the container -----
|
||||
Write-Info "Démarrage du conteneur..."
|
||||
docker compose -f $ComposeFile up -d --remove-orphans
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Error "Échec du démarrage du conteneur."
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
Write-Ok "Conteneur démarré"
|
||||
|
||||
# ----- Show status & tip -----
|
||||
Write-Host ""
|
||||
Write-Info "Statut du conteneur :"
|
||||
docker compose -f $ComposeFile ps
|
||||
|
||||
Write-Host ""
|
||||
Write-Info "Application accessible sur http://localhost:2020"
|
||||
if ($EnvMode -eq "prod") {
|
||||
Write-Info "Login : admin / (mot de passe dans .env)"
|
||||
} else {
|
||||
Write-Info "Login : admin / test123"
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
Write-Info "Logs (Ctrl+C pour quitter) :"
|
||||
docker compose -f $ComposeFile logs --tail=20 -f
|
||||
@@ -4,7 +4,12 @@
|
||||
# =============================================================================
|
||||
#
|
||||
# Usage :
|
||||
# ./build.sh [OPTIONS]
|
||||
# ./build.sh [--prod | --test-win | --test-linux] [OPTIONS]
|
||||
#
|
||||
# Environnements :
|
||||
# --prod Production (défaut) — docker-compose.yml
|
||||
# --test-win Test Windows (Docker Desktop) — docker-compose.test-win.yml
|
||||
# --test-linux Test Linux — docker-compose.test-linux.yml
|
||||
#
|
||||
# Options :
|
||||
# --help, -h Affiche cette aide
|
||||
@@ -16,17 +21,17 @@
|
||||
#
|
||||
# Étapes exécutées :
|
||||
# 1. Vérifie que Docker et Docker Compose sont installés
|
||||
# 2. Valide le fichier docker-compose.yml
|
||||
# 3. Vérifie que les volumes montés existent sur le host
|
||||
# 2. Valide le fichier docker-compose choisi
|
||||
# 3. Crée les dossiers de test si nécessaire (modes test uniquement)
|
||||
# 4. Construit l'image Docker (multi-stage, ~180MB)
|
||||
# 5. Démarre le conteneur en arrière-plan
|
||||
# 6. Affiche le statut et les logs
|
||||
#
|
||||
# Exemples :
|
||||
# ./build.sh # Build from scratch + démarrage
|
||||
# ./build.sh --cache # Build avec cache + démarrage
|
||||
# ./build.sh --build-only # Build uniquement, sans démarrer
|
||||
# ./build.sh --progress=plain # Sortie verbeuse
|
||||
# ./build.sh # Production
|
||||
# ./build.sh --test-win # Test Windows
|
||||
# ./build.sh --test-linux --cache # Test Linux avec cache
|
||||
# ./build.sh --test-win --build-only # Build test Windows sans démarrer
|
||||
# =============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
@@ -43,6 +48,8 @@ show_help() {
|
||||
}
|
||||
|
||||
# ----- Parse args -----
|
||||
ENV_MODE="prod"
|
||||
COMPOSE_FILE=""
|
||||
BUILD_ONLY=false
|
||||
USE_CACHE=false
|
||||
BUILD_ARGS=()
|
||||
@@ -52,6 +59,15 @@ for arg in "$@"; do
|
||||
--help|-h)
|
||||
show_help
|
||||
;;
|
||||
--prod)
|
||||
ENV_MODE="prod"
|
||||
;;
|
||||
--test-win)
|
||||
ENV_MODE="test-win"
|
||||
;;
|
||||
--test-linux)
|
||||
ENV_MODE="test-linux"
|
||||
;;
|
||||
--build-only)
|
||||
BUILD_ONLY=true
|
||||
;;
|
||||
@@ -67,6 +83,26 @@ for arg in "$@"; do
|
||||
esac
|
||||
done
|
||||
|
||||
# ----- Select compose file -----
|
||||
case "$ENV_MODE" in
|
||||
prod)
|
||||
COMPOSE_FILE="docker-compose.yml"
|
||||
COMPOSE_ENV_NAME="production"
|
||||
;;
|
||||
test-win)
|
||||
COMPOSE_FILE="docker-compose.test-win.yml"
|
||||
COMPOSE_ENV_NAME="test Windows"
|
||||
;;
|
||||
test-linux)
|
||||
COMPOSE_FILE="docker-compose.test-linux.yml"
|
||||
COMPOSE_ENV_NAME="test Linux"
|
||||
;;
|
||||
*)
|
||||
error "Environnement inconnu : $ENV_MODE"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# ----- Pre-flight checks -----
|
||||
if ! command -v docker &>/dev/null; then
|
||||
error "Docker est introuvable. Installez Docker avant de continuer."
|
||||
@@ -87,21 +123,46 @@ fi
|
||||
ok "$($COMPOSE_CMD version --short 2>/dev/null || $COMPOSE_CMD version | head -1)"
|
||||
|
||||
# ----- Validate compose file -----
|
||||
COMPOSE_FILE="docker-compose.yml"
|
||||
if [[ ! -f "$COMPOSE_FILE" ]]; then
|
||||
error "$COMPOSE_FILE introuvable dans $(pwd)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
info "=== ObsiGate — Build & Deploy ==="
|
||||
echo ""
|
||||
info "=== ObsiGate — Build & Deploy ($COMPOSE_ENV_NAME) ==="
|
||||
info "Fichier compose : $COMPOSE_FILE"
|
||||
|
||||
# ----- Extract volume mount paths from compose file and warn about missing ones -----
|
||||
# ----- Test mode: create test directories -----
|
||||
if [[ "$ENV_MODE" == "test-win" ]]; then
|
||||
info "Création des dossiers de test Windows..."
|
||||
mkdir -p /c/dev/git/python/ObsiGate/test_vault
|
||||
mkdir -p /c/dev/git/python/ObsiGate/test_dir
|
||||
# Create a sample note for the test vault
|
||||
if [[ ! -f /c/dev/git/python/ObsiGate/test_vault/Accueil.md ]]; then
|
||||
echo "# Bienvenue dans le vault de test" > /c/dev/git/python/ObsiGate/test_vault/Accueil.md
|
||||
echo "" >> /c/dev/git/python/ObsiGate/test_vault/Accueil.md
|
||||
echo "Ce vault est utilisé pour les tests de développement d'ObsiGate." >> /c/dev/git/python/ObsiGate/test_vault/Accueil.md
|
||||
ok "Note de test créée : test_vault/Accueil.md"
|
||||
fi
|
||||
elif [[ "$ENV_MODE" == "test-linux" ]]; then
|
||||
info "Création des dossiers de test Linux..."
|
||||
mkdir -p /tmp/obsigate-test-vault
|
||||
mkdir -p /tmp/obsigate-test-dir
|
||||
mkdir -p /tmp/obsigate-test-data
|
||||
chmod -R 777 /tmp/obsigate-test-data 2>/dev/null || true
|
||||
if [[ ! -f /tmp/obsigate-test-vault/Accueil.md ]]; then
|
||||
echo "# Bienvenue dans le vault de test" > /tmp/obsigate-test-vault/Accueil.md
|
||||
echo "" >> /tmp/obsigate-test-vault/Accueil.md
|
||||
echo "Ce vault est utilisé pour les tests de développement d'ObsiGate." >> /tmp/obsigate-test-vault/Accueil.md
|
||||
ok "Note de test créée : test-vault/Accueil.md"
|
||||
fi
|
||||
fi
|
||||
|
||||
# ----- Extract volume mount paths and warn about missing ones -----
|
||||
info "Vérification des volumes montés sur le host..."
|
||||
VOLUME_COUNT=0
|
||||
MISSING_COUNT=0
|
||||
while IFS= read -r line; do
|
||||
# Lines like: - /host/path:/container/path
|
||||
if [[ "$line" =~ ^[[:space:]]*-[[:space:]]+(/[^:]+): ]]; then
|
||||
host_path="${BASH_REMATCH[1]}"
|
||||
VOLUME_COUNT=$((VOLUME_COUNT + 1))
|
||||
@@ -115,7 +176,7 @@ while IFS= read -r line; do
|
||||
done < <(grep -E '^\s+-\s+/' "$COMPOSE_FILE")
|
||||
|
||||
if [[ "$VOLUME_COUNT" -eq 0 ]]; then
|
||||
warn "Aucun volume host détecté dans $COMPOSE_FILE. Pensez à configurer vos vaults."
|
||||
warn "Aucun volume host détecté dans $COMPOSE_FILE."
|
||||
fi
|
||||
if [[ "$MISSING_COUNT" -gt 0 ]]; then
|
||||
warn "$MISSING_COUNT volume(s) absent(s). Vérifiez vos chemins dans $COMPOSE_FILE."
|
||||
@@ -132,27 +193,34 @@ else
|
||||
info "Construction de l'image Docker (avec cache)..."
|
||||
fi
|
||||
|
||||
$COMPOSE_CMD build "${BUILD_FLAGS[@]}" "${BUILD_ARGS[@]}"
|
||||
# Version livrée : le fichier VERSION (racine du dépôt) est la source unique de
|
||||
# vérité ; le Dockerfile le copie dans l'image. Aucun numéro codé en dur ici.
|
||||
VERSION=$(cat VERSION 2>/dev/null | tr -d '[:space:]' | sed 's/^v//' || true)
|
||||
[ -n "$VERSION" ] || VERSION="0.0.0"
|
||||
export VERSION
|
||||
info "Version: $VERSION"
|
||||
|
||||
$COMPOSE_CMD -f "$COMPOSE_FILE" build "${BUILD_FLAGS[@]}" "${BUILD_ARGS[@]}"
|
||||
ok "Image construite avec succès"
|
||||
|
||||
# ----- Stop if build-only -----
|
||||
if [[ "$BUILD_ONLY" == true ]]; then
|
||||
echo ""
|
||||
info "Mode build-only : conteneur non démarré."
|
||||
info "Pour démarrer manuellement : $COMPOSE_CMD up -d"
|
||||
info "Pour démarrer manuellement : $COMPOSE_CMD -f $COMPOSE_FILE up -d"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ----- Start the container -----
|
||||
info "Démarrage du conteneur..."
|
||||
$COMPOSE_CMD up -d --remove-orphans
|
||||
$COMPOSE_CMD -f "$COMPOSE_FILE" up -d --remove-orphans
|
||||
ok "Conteneur démarré"
|
||||
|
||||
# ----- Show status -----
|
||||
echo ""
|
||||
info "Statut du conteneur :"
|
||||
$COMPOSE_CMD ps
|
||||
$COMPOSE_CMD -f "$COMPOSE_FILE" ps
|
||||
|
||||
echo ""
|
||||
info "Logs récents (Ctrl+C pour quitter) :"
|
||||
$COMPOSE_CMD logs --tail=20 -f
|
||||
$COMPOSE_CMD -f "$COMPOSE_FILE" logs --tail=20 -f
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
# Code Context — ObsiGate Bug Investigation
|
||||
|
||||
> **⚠️ Note de workflow (obsolète pour cette investigation)** : la méthode de livraison
|
||||
> obligatoire du dépôt est définie dans [`AGENTS.md`](./AGENTS.md) et
|
||||
> [`docs/DELIVERY_WORKFLOW.md`](./docs/DELIVERY_WORKFLOW.md). À appliquer pour toute nouvelle
|
||||
> tâche ; le contenu ci-dessous est un artefact d'investigation conservé pour historique.
|
||||
|
||||
## Files Retrieved
|
||||
1. `frontend/app.js` (lines 5585–5665) — `showWelcome()` rebuilds dashboard HTML with only bookmarks + recent sections
|
||||
2. `frontend/index.html` (lines 360–406) — Initial dashboard DOM has all 4 sections: stats, bookmarks, conflicts, recent
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"version": 1,
|
||||
"users": {
|
||||
"admin": {
|
||||
"id": "f7b85dfa-bcf6-4e42-b861-7563673620a6",
|
||||
"username": "admin",
|
||||
"display_name": "admin",
|
||||
"password_hash": "$argon2id$v=19$m=65536,t=2,p=2$H4Pa7H4HOWeQoEbj4Ndkbw$c61e9zRHuXXdrLB6uTrjIBo/FFQaaZMaNHkB7CZaHhM",
|
||||
"role": "admin",
|
||||
"vaults": [
|
||||
"*"
|
||||
],
|
||||
"active": true,
|
||||
"created_at": "2026-03-23T19:38:00.742597+00:00",
|
||||
"last_login": null,
|
||||
"failed_attempts": 5,
|
||||
"locked_until": "2026-06-02T14:01:43.472937+00:00"
|
||||
}
|
||||
}
|
||||
}
|
||||
Generated
+6047
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,45 @@
|
||||
[package]
|
||||
name = "obsigate-desktop"
|
||||
version = "2.4.0"
|
||||
description = "ObsiGate Desktop — Porte d'entrée native pour vos vaults Obsidian"
|
||||
authors = ["Bruno Charest"]
|
||||
edition = "2021"
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = [] }
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "2", features = ["tray-icon"] }
|
||||
tauri-plugin-shell = "2"
|
||||
tauri-plugin-dialog = "2"
|
||||
tauri-plugin-notification = "2"
|
||||
tauri-plugin-fs = "2"
|
||||
tauri-plugin-process = "2"
|
||||
tauri-plugin-store = "2"
|
||||
tauri-plugin-single-instance = "2"
|
||||
tauri-plugin-updater = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
reqwest = { version = "0.12", features = ["json"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
log = "0.4"
|
||||
env_logger = "0.11"
|
||||
chrono = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows = { version = "0.61", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_System_Com",
|
||||
"Win32_System_Com_StructuredStorage",
|
||||
"Win32_System_Variant",
|
||||
"Win32_UI_Shell",
|
||||
"Win32_UI_Shell_Common",
|
||||
"Win32_UI_Shell_PropertiesSystem",
|
||||
] }
|
||||
|
||||
[features]
|
||||
default = ["custom-protocol"]
|
||||
custom-protocol = ["tauri/custom-protocol"]
|
||||
@@ -0,0 +1,368 @@
|
||||
# ObsiGate Desktop
|
||||
|
||||
Application desktop native pour [ObsiGate](https://git.dracodev.net/Projets/ObsiGate) — construite avec [Tauri](https://tauri.app/) (Rust + webview système).
|
||||
|
||||
> 🚧 **Version 2.0.0 — les binaires sont en cours de stabilisation.** Le build depuis les sources est la méthode recommandée pour le moment.
|
||||
|
||||
## Table des matières
|
||||
|
||||
- [Installation (binaires pré-buildés)](#installation-binaires-pre-buildes)
|
||||
- [Build depuis les sources](#build-depuis-les-sources)
|
||||
- [Démarrage](#demarrage)
|
||||
- [Configuration des vaults](#configuration-des-vaults)
|
||||
- [Architecture](#architecture)
|
||||
- [Fonctionnalités natives](#fonctionnalites-natives)
|
||||
- [Signature de code Windows](#signature-de-code-windows)
|
||||
- [Dépannage](#depannage)
|
||||
|
||||
---
|
||||
|
||||
## Installation (binaires pré-buildés)
|
||||
|
||||
Les releases sont publiées sur [Gitea](https://git.dracodev.net/Projets/ObsiGate/releases).
|
||||
|
||||
### Linux
|
||||
|
||||
```bash
|
||||
# .deb (Debian/Ubuntu/Deepin) — installation système
|
||||
sudo dpkg -i obsigate_2.0.0_amd64.deb
|
||||
# Lancement : menu applications → ObsiGate, ou :
|
||||
obsigate-desktop
|
||||
|
||||
# .AppImage (toute distribution) — portable, pas d'installation
|
||||
chmod +x ObsiGate_2.0.0_amd64.AppImage
|
||||
./ObsiGate_2.0.0_amd64.AppImage
|
||||
```
|
||||
|
||||
Dépendances runtime (normalement déjà présentes) :
|
||||
```bash
|
||||
sudo apt install libwebkit2gtk-4.1-0 libgtk-3-0 libayatana-appindicator3-1
|
||||
```
|
||||
|
||||
### Windows
|
||||
|
||||
```cmd
|
||||
:: .msi — installation standard
|
||||
:: Double-cliquer sur ObsiGate_2.0.0_x64.msi
|
||||
:: → Crée un raccourci bureau + entrée menu Démarrer
|
||||
|
||||
:: .exe NSIS — installateur interactif
|
||||
:: Mêmes options, inclut le raccourci
|
||||
```
|
||||
|
||||
> **Binaires non signés** : les releases ne sont pas signées avec un certificat
|
||||
> Windows. Au premier lancement, SmartScreen affiche « Windows a protégé votre
|
||||
> PC » → cliquer **Informations complémentaires → Exécuter quand même**.
|
||||
> Voir [Signature de code](#signature-de-code-windows) pour les alternatives.
|
||||
|
||||
### macOS
|
||||
|
||||
> Non supporté pour le moment (priorité Linux/Windows).
|
||||
|
||||
---
|
||||
|
||||
## Build depuis les sources
|
||||
|
||||
### Prérequis communs
|
||||
|
||||
- [Rust](https://rustup.rs/) stable ≥ 1.75
|
||||
- Tauri CLI : `cargo install tauri-cli`
|
||||
- Python 3.11+
|
||||
- Git
|
||||
|
||||
### Linux
|
||||
|
||||
```bash
|
||||
# 1. Dépendances système
|
||||
sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev curl
|
||||
|
||||
# 2. Cloner le projet
|
||||
git clone https://git.dracodev.net/Projets/ObsiGate.git
|
||||
cd ObsiGate/desktop
|
||||
|
||||
# 3. Build (tout-en-un)
|
||||
chmod +x build-linux.sh
|
||||
./build-linux.sh
|
||||
```
|
||||
|
||||
Le script s'occupe de :
|
||||
1. Vérifier Rust + Tauri CLI + dépendances système
|
||||
2. Créer l'environnement Python (venv + requirements)
|
||||
3. Copier le frontend
|
||||
4. Builder Tauri en mode release
|
||||
|
||||
Produits :
|
||||
- `target/release/bundle/deb/obsigate_2.0.0_amd64.deb`
|
||||
- `target/release/bundle/appimage/ObsiGate_2.0.0_amd64.AppImage`
|
||||
|
||||
### Windows (via Scoop)
|
||||
|
||||
```powershell
|
||||
# 1. Installer Scoop (si pas déjà fait)
|
||||
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
|
||||
irm get.scoop.sh | iex
|
||||
|
||||
# 2. Prérequis via Scoop
|
||||
scoop install rustup curl git
|
||||
rustup default stable
|
||||
cargo install tauri-cli
|
||||
|
||||
# 3. Cloner
|
||||
git clone https://git.dracodev.net/Projets/ObsiGate.git
|
||||
cd ObsiGate\desktop
|
||||
|
||||
# 4. Build
|
||||
.\build-windows.bat
|
||||
```
|
||||
|
||||
Le script télécharge automatiquement Python 3.11 embed et les dépendances backend.
|
||||
|
||||
Produits :
|
||||
- `target\release\bundle\msi\ObsiGate_2.0.0_x64.msi`
|
||||
- `target\release\bundle\nsis\ObsiGate_2.0.0_x64-setup.exe`
|
||||
|
||||
---
|
||||
|
||||
## Démarrage
|
||||
|
||||
### Premier lancement
|
||||
|
||||
1. **Lance l'application** (menu ou ligne de commande)
|
||||
- 💡 *Premier lancement recommandé depuis un terminal* pour voir le mot de passe admin généré
|
||||
2. Le backend Python démarre automatiquement → health check sur `127.0.0.1:17890`
|
||||
3. La fenêtre s'ouvre sur l'interface ObsiGate (écran de connexion si l'auth est activée)
|
||||
4. **Connecte-toi** avec le compte admin créé automatiquement (voir [Compte administrateur](#compte-administrateur))
|
||||
5. **Sélectionne le dossier parent de tes vaults** Obsidian via le sélecteur natif
|
||||
6. L'indexation démarre automatiquement
|
||||
|
||||
### Compte administrateur
|
||||
|
||||
Au premier démarrage, un compte administrateur est créé automatiquement :
|
||||
|
||||
- **Login** : `admin` (modifiable via la variable d'environnement `OBSIGATE_ADMIN_USER`)
|
||||
- **Mot de passe** : généré aléatoirement (16 caractères) et affiché **une seule fois** dans les logs
|
||||
|
||||
Le mot de passe apparaît dans la sortie du terminal sous cette forme :
|
||||
```
|
||||
============================================================
|
||||
PREMIER DÉMARRAGE — Compte admin créé automatiquement
|
||||
Utilisateur : admin
|
||||
Mot de passe : XXXXXXXXXXXXXXXX
|
||||
CHANGEZ CE MOT DE PASSE dès la première connexion !
|
||||
============================================================
|
||||
```
|
||||
|
||||
> ⚠️ **Note le mot de passe immédiatement** — il n'est affiché qu'une seule fois.
|
||||
> Si tu rates les logs, supprime le dossier `data/` et relance l'application pour
|
||||
> régénérer un nouveau compte.
|
||||
|
||||
**Prédéfinir le mot de passe** (recommandé) :
|
||||
|
||||
Avant le premier lancement, définis les variables d'environnement suivantes :
|
||||
|
||||
- **Windows** (PowerShell) : `$env:OBSIGATE_ADMIN_PASSWORD="MonMotDePasse"`
|
||||
- **Linux** : `export OBSIGATE_ADMIN_PASSWORD="MonMotDePasse"`
|
||||
|
||||
Tu peux aussi définir `OBSIGATE_ADMIN_USER` pour changer le nom d'utilisateur.
|
||||
|
||||
### Lancements suivants
|
||||
|
||||
- L'application mémorise le dernier dossier configuré
|
||||
- Le backend démarre en ~2 secondes
|
||||
- Les vaults sont ré-indexées au démarrage (incrémental si inchangé)
|
||||
|
||||
### Arrêt
|
||||
|
||||
- **Icône tray → Quitter** (recommandé) : SIGTERM → arrêt propre du backend → fermeture webview
|
||||
- **Fermeture fenêtre (X)** : minimise dans le tray (backend continue de tourner)
|
||||
- **Ctrl+C** dans le terminal (si lancé en dev) : arrêt immédiat
|
||||
|
||||
### Mode développement
|
||||
|
||||
```bash
|
||||
cd desktop
|
||||
cargo tauri dev
|
||||
# → Lance le backend Python + ouvre la webview avec hot-reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Configuration des vaults
|
||||
|
||||
Au premier lancement, le sélecteur de dossier natif te demande le **dossier parent** contenant tes vaults Obsidian. Exemple :
|
||||
|
||||
```
|
||||
/home/bruno/Documents/
|
||||
├── Obsidian-Recettes/ ← vault #1
|
||||
├── Obsidian-IT/ ← vault #2
|
||||
└── Obsidian-Perso/ ← vault #3
|
||||
```
|
||||
|
||||
Tous les sous-dossiers contenant un dossier `.obsidian` sont automatiquement détectés comme vaults.
|
||||
|
||||
### Configuration avancée
|
||||
|
||||
Le fichier de config desktop est stocké dans :
|
||||
- **Linux** : `~/.config/obsigate-desktop/config.json`
|
||||
- **Windows** : `%APPDATA%\ObsiGate\config.json`
|
||||
|
||||
```json
|
||||
{
|
||||
"vaults_path": "/home/bruno/Documents",
|
||||
"backend_port": 17890,
|
||||
"theme": "system"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
desktop/
|
||||
├── Cargo.toml # Dépendances Rust (Tauri 2, plugins)
|
||||
├── tauri.conf.json # Config Tauri (fenêtre, bundle, plugins)
|
||||
├── build.rs # Script build Tauri
|
||||
├── src/
|
||||
│ └── main.rs # Point d'entrée Rust : spawn backend + webview
|
||||
├── python-embed/
|
||||
│ ├── venv/ # Environnement Python isolé
|
||||
│ └── ... # site-packages gelés
|
||||
├── build-linux.sh # Script build Linux (.deb + .AppImage)
|
||||
├── build-windows.bat # Script build Windows (.msi + NSIS)
|
||||
└── icons/ # Icônes desktop (.ico, .png, .icns)
|
||||
```
|
||||
|
||||
### Cycle de vie
|
||||
|
||||
```
|
||||
Démarrage :
|
||||
1. Tauri lance python-embed/venv/bin/python → uvicorn backend.main:app
|
||||
2. Health check GET /api/health (timeout 15s)
|
||||
3. Webview ouvre http://127.0.0.1:17890
|
||||
|
||||
Arrêt (tray → Quitter ou Ctrl+C) :
|
||||
1. SIGTERM envoyé au processus Python
|
||||
2. Backend s'arrête proprement (10s timeout)
|
||||
3. Tauri ferme la webview
|
||||
4. Processus nettoyé — aucun résidu
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Fonctionnalités desktop natives
|
||||
|
||||
| Fonctionnalité | Web (Docker) | Desktop (Tauri) |
|
||||
|---|---|---|
|
||||
| Accès fichiers local | Via upload | Natif (sélecteur dossier) |
|
||||
| Thème | Manuel (toggle) | Auto (suit OS dark/light) |
|
||||
| Notifications | Service Worker | Natif OS |
|
||||
| Associations `.md` | ❌ | ✅ « Ouvrir avec ObsiGate » |
|
||||
| Tray icon | ❌ | ✅ Barre des tâches |
|
||||
| Auto-update | ❌ (docker pull) | ✅ Vérifie releases Gitea |
|
||||
| Mode hors-ligne | Limité | Complet (backend local) |
|
||||
| RAM au repos | ~150 MB (Docker) | ~80 MB (natif) |
|
||||
| Démarrage à froid | ~5s (Docker) | ~2s |
|
||||
|
||||
---
|
||||
|
||||
## Signature de code Windows
|
||||
|
||||
La signature de code est **optionnelle** : sans elle, l'application fonctionne,
|
||||
mais SmartScreen affiche un avertissement au premier lancement. Les binaires
|
||||
ObsiGate sont actuellement distribués **non signés**.
|
||||
|
||||
### Signer localement
|
||||
|
||||
Le script `scripts/sign-windows.ps1` signe le binaire et les installeurs après un
|
||||
`cargo tauri build`. Il lit les identifiants depuis l'environnement (jamais
|
||||
commités) et est un **no-op explicite** si aucun certificat n'est fourni :
|
||||
|
||||
```powershell
|
||||
$env:OBSIGATE_SIGN_CERT_PFX = "C:\certs\obsigate.pfx"
|
||||
$env:OBSIGATE_SIGN_CERT_PASSWORD = "..."
|
||||
$env:OBSIGATE_SIGN_TIMESTAMP_URL = "http://timestamp.digicert.com"
|
||||
.\scripts\sign-windows.ps1
|
||||
```
|
||||
|
||||
### Alternatives au certificat
|
||||
|
||||
| Option | Coût indicatif | Effet SmartScreen |
|
||||
|---|---|---|
|
||||
| **Livrer non signé** (statu quo) | 0 € | Avertissement → « Exécuter quand même » |
|
||||
| **SignPath.io** (projet open source) | Gratuit si éligible OSS | Réputation gérée par le service |
|
||||
| **Certum Open Source Code Signing** | ~70-100 €/an | Réputation progressive |
|
||||
| **Certificat OV** | ~150-400 €/an | Avertit tant que la réputation n'est pas établie |
|
||||
| **Certificat EV** | ~300-700 €/an + token USB/HSM | Réputation **immédiate** |
|
||||
| **Azure Trusted Signing** | ~10 $/mois | Bonne réputation, signature cloud |
|
||||
| **Certificat auto-signé** | 0 € | Inutile en distribution publique |
|
||||
|
||||
### Signature de l'auto-update (gratuite)
|
||||
|
||||
Indépendante de la signature Windows, la signature des mises à jour Tauri repose
|
||||
sur une paire de clés que vous générez vous-même :
|
||||
|
||||
```bash
|
||||
cargo tauri signer generate -w obsigate-updater.key
|
||||
```
|
||||
|
||||
- La **clé publique** est déjà renseignée dans `plugins.updater.pubkey`
|
||||
(`tauri.conf.json`).
|
||||
- La **clé privée** (`desktop/obsigate-updater.key`, gitignorée) est lue
|
||||
automatiquement par `build-windows.bat` / `build-linux.sh` ; en CI, via les
|
||||
secrets Gitea `TAURI_SIGNING_PRIVATE_KEY` / `TAURI_SIGNING_PRIVATE_KEY_PASSWORD`.
|
||||
- Le CLI produit des `.sig` par artefact (`*.exe.sig`, `*.AppImage.sig`, …).
|
||||
|
||||
**Manifeste `latest.json`** (détection des mises à jour) :
|
||||
|
||||
```powershell
|
||||
python scripts\updater_manifest.py --tag vX.Y.Z # ou via publish_release.py
|
||||
```
|
||||
|
||||
`publish_release.py` le génère et l'ajoute aux assets ; il reste à **committer
|
||||
`desktop/latest.json` sur `main`**. L'updater lit ce fichier versionné :
|
||||
`https://git.dracodev.net/Projets/ObsiGate/raw/branch/main/desktop/latest.json`.
|
||||
Détail : [DEVELOPMENT_AND_RELEASES §2bis](../docs/DEVELOPMENT_AND_RELEASES.md#2bis-signature-des-mises-à-jour-updater-tauri).
|
||||
|
||||
---
|
||||
|
||||
## Dépannage
|
||||
|
||||
### Le backend ne démarre pas
|
||||
|
||||
```bash
|
||||
# Vérifier que le port 17890 est libre
|
||||
ss -tlnp | grep 17890
|
||||
|
||||
# Lancer le backend manuellement pour voir les logs
|
||||
cd ObsiGate
|
||||
python -m uvicorn backend.main:app --host 127.0.0.1 --port 17890 --log-level debug
|
||||
```
|
||||
|
||||
### La fenêtre reste blanche
|
||||
|
||||
- Vérifier que le backend répond : `curl http://127.0.0.1:17890/api/health`
|
||||
- Si "connection refused" → le backend n'a pas démarré (voir section précédente)
|
||||
- Si le healthcheck répond mais la webview est blanche → problème CSP ou webview
|
||||
|
||||
### Erreur libwebkit2gtk non trouvé
|
||||
|
||||
```bash
|
||||
# Ubuntu/Debian/Deepin
|
||||
sudo apt install libwebkit2gtk-4.1-0
|
||||
|
||||
# Si l'erreur persiste, installer la version dev
|
||||
sudo apt install libwebkit2gtk-4.1-dev
|
||||
```
|
||||
|
||||
### Build échoue sur Linux
|
||||
|
||||
```bash
|
||||
# Nettoyer et rebuild
|
||||
cargo clean
|
||||
rm -rf python-embed/venv
|
||||
./build-linux.sh
|
||||
|
||||
# Si erreur de linking, vérifier les -dev packages
|
||||
sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev libssl-dev
|
||||
```
|
||||
Executable
+75
@@ -0,0 +1,75 @@
|
||||
#!/bin/bash
|
||||
# ============================================================
|
||||
# ObsiGate Desktop — Build Linux (.deb + .AppImage)
|
||||
# Prérequis : Rust, Tauri CLI, libwebkit2gtk, Python 3.11
|
||||
# ============================================================
|
||||
set -e
|
||||
|
||||
echo "========================================"
|
||||
echo " ObsiGate Desktop — Build Linux"
|
||||
echo "========================================"
|
||||
echo ""
|
||||
|
||||
# ── 1. Vérifier les prérequis ─────────────────────────────────
|
||||
echo "[1/5] Verifying prerequisites..."
|
||||
command -v cargo >/dev/null 2>&1 || { echo "❌ Rust/Cargo not found. Install: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"; exit 1; }
|
||||
command -v tauri >/dev/null 2>&1 || cargo install tauri-cli
|
||||
|
||||
# Check system deps
|
||||
if command -v apt-get >/dev/null 2>&1; then
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev curl 2>/dev/null
|
||||
fi
|
||||
echo "✅ Dependencies OK"
|
||||
|
||||
# ── 2. Préparer Python 3.11 ────────────────────────────────────
|
||||
echo "[2/5] Preparing Python environment..."
|
||||
python3 -m venv python-embed/venv
|
||||
source python-embed/venv/bin/activate
|
||||
pip install --upgrade pip -q
|
||||
pip install -r ../backend/requirements.txt -q
|
||||
echo "✅ Python environment ready"
|
||||
|
||||
# ── 2b. Stage backend + frontend pour le bundle ───────────────
|
||||
# tauri.conf.json embarque backend/** et frontend/** depuis desktop/ :
|
||||
# sans ce staging, tauri_build échoue (« glob pattern backend/**/* path not found »).
|
||||
echo "[2b/5] Staging backend + frontend for bundle..."
|
||||
rm -rf backend frontend
|
||||
cp -r ../backend backend
|
||||
cp -r ../frontend frontend
|
||||
echo "✅ Staged"
|
||||
|
||||
# ── 2c. Clé de signature des mises à jour (updater Tauri) ──────
|
||||
if [ -f "obsigate-updater.key" ]; then
|
||||
export TAURI_SIGNING_PRIVATE_KEY="$(cat obsigate-updater.key)"
|
||||
SIGN_CONFIG=""
|
||||
echo "[2c/5] Clé updater trouvée — artefacts .sig activés"
|
||||
else
|
||||
SIGN_CONFIG='--config {"bundle":{"createUpdaterArtifacts":false}}'
|
||||
echo "[2c/5] Clé updater absente — build sans .sig"
|
||||
fi
|
||||
|
||||
# ── 3. Build Tauri ────────────────────────────────────────────
|
||||
echo "[3/5] Building Tauri application..."
|
||||
cargo tauri build --target x86_64-unknown-linux-gnu --bundles deb,appimage $SIGN_CONFIG
|
||||
echo "✅ Build successful"
|
||||
|
||||
# ── 4. Copier le runtime à côté de l'exécutable ───────────────
|
||||
OUTDIR="target/x86_64-unknown-linux-gnu/release"
|
||||
echo "[4/5] Copying runtime to $OUTDIR..."
|
||||
rm -rf "$OUTDIR/python-embed" "$OUTDIR/backend" "$OUTDIR/frontend"
|
||||
cp -r python-embed "$OUTDIR/"
|
||||
cp -r ../backend "$OUTDIR/"
|
||||
cp -r ../frontend "$OUTDIR/"
|
||||
echo "✅ Runtime + frontend copied"
|
||||
|
||||
# ── 4b. Nettoyer les dossiers stagés ──────────────────────────
|
||||
rm -rf backend frontend
|
||||
echo "✅ Cleaned"
|
||||
|
||||
# ── 5. Résultats ───────────────────────────────────────────────
|
||||
OUTDIR="target/x86_64-unknown-linux-gnu/release"
|
||||
echo "[5/5] Results:"
|
||||
ls -lh "$OUTDIR/bundle/deb/"*.deb 2>/dev/null
|
||||
ls -lh "$OUTDIR/bundle/appimage/"*.AppImage 2>/dev/null
|
||||
echo ""
|
||||
echo "✅ Done! Check $OUTDIR/bundle/"
|
||||
@@ -0,0 +1,100 @@
|
||||
@echo off
|
||||
REM ============================================================
|
||||
REM ObsiGate Desktop — Build Windows (NSIS installer)
|
||||
REM Prérequis : Rust, Tauri CLI, NSIS, Python 3.11 embed
|
||||
REM ============================================================
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
echo ========================================
|
||||
echo ObsiGate Desktop — Build Windows
|
||||
echo ========================================
|
||||
echo.
|
||||
|
||||
REM ── 0. Kill any leftover backend from a previous run ──────────
|
||||
echo [0/6] Killing leftover Python processes...
|
||||
taskkill /F /IM python.exe 2>nul
|
||||
echo.
|
||||
|
||||
REM ── 1. Vérifier les prérequis ─────────────────────────────────
|
||||
echo [1/6] Verifying prerequisites...
|
||||
where cargo >nul 2>&1 || (echo ❌ Rust/Cargo not found. Install from https://rustup.rs & exit /b 1)
|
||||
where tauri >nul 2>&1 || cargo install tauri-cli
|
||||
echo ✅ Rust + Tauri OK
|
||||
|
||||
REM ── 2. Préparer Python 3.11 embed ─────────────────────────────
|
||||
echo [2/6] Preparing Python embed...
|
||||
echo 📥 Downloading Python 3.11 embed...
|
||||
if exist "python-embed" rmdir /s /q "python-embed"
|
||||
curl -L -o python-embed.zip "https://www.python.org/ftp/python/3.11.9/python-3.11.9-embed-amd64.zip"
|
||||
powershell Expand-Archive -Path python-embed.zip -DestinationPath python-embed\
|
||||
del python-embed.zip
|
||||
|
||||
REM Enable pip and site-packages + parent dir for 'import backend'
|
||||
echo import site >> python-embed\python311._pth
|
||||
echo ..>> python-embed\python311._pth
|
||||
|
||||
REM Download get-pip.py (embed Python doesn't include ensurepip)
|
||||
echo 📥 Downloading get-pip.py...
|
||||
curl -L -o get-pip.py "https://bootstrap.pypa.io/get-pip.py"
|
||||
python-embed\python.exe get-pip.py --no-cache-dir
|
||||
del get-pip.py
|
||||
|
||||
REM Install dependencies
|
||||
python-embed\python.exe -m pip install --no-cache-dir -r ..\backend\requirements.txt
|
||||
echo ✅ Python embed ready
|
||||
|
||||
REM ── 2b. Copier backend + frontend localement pour le bundle ────
|
||||
echo [2b/6] Staging backend + frontend for bundle...
|
||||
if exist "backend" rmdir /s /q "backend"
|
||||
if exist "frontend" rmdir /s /q "frontend"
|
||||
xcopy /E /I /Q /Y "..\backend" "backend"
|
||||
xcopy /E /I /Q /Y "..\frontend" "frontend"
|
||||
echo ✅ Staged
|
||||
|
||||
REM ── 2c. Clé de signature des mises à jour (updater Tauri) ──────
|
||||
set "SIGN_CONFIG="
|
||||
if exist "obsigate-updater.key" (
|
||||
set /p TAURI_SIGNING_PRIVATE_KEY=<obsigate-updater.key
|
||||
echo [2c/6] Cle updater trouvee - artefacts .sig actives
|
||||
) else (
|
||||
> updater-off.json echo {"bundle":{"createUpdaterArtifacts":false}}
|
||||
set "SIGN_CONFIG=--config updater-off.json"
|
||||
echo [2c/6] Cle updater absente - build sans .sig
|
||||
)
|
||||
|
||||
REM ── 3. Build Tauri ────────────────────────────────────────────
|
||||
echo [3/6] Building Tauri application (NSIS + MSI installers)...
|
||||
cargo tauri build --target x86_64-pc-windows-msvc --bundles nsis,msi %SIGN_CONFIG%
|
||||
if errorlevel 1 (
|
||||
echo ⚠️ Standard bundle build failed, trying fallback to NSIS only...
|
||||
cargo tauri build --target x86_64-pc-windows-msvc --bundles nsis %SIGN_CONFIG%
|
||||
if errorlevel 1 (
|
||||
echo ❌ Build failed!
|
||||
goto :cleanup
|
||||
)
|
||||
)
|
||||
echo ✅ Build successful
|
||||
|
||||
REM ── 4. Copier python-embed vers output dir (dev local) ─────────
|
||||
echo [4/6] Copying python-embed to output dir...
|
||||
set "OUTDIR=target\x86_64-pc-windows-msvc\release"
|
||||
if exist "%OUTDIR%\python-embed" rmdir /s /q "%OUTDIR%\python-embed" 2>nul
|
||||
xcopy /E /I /Q /Y "python-embed" "%OUTDIR%\python-embed"
|
||||
echo ..>> "%OUTDIR%\python-embed\python311._pth"
|
||||
echo ✅ Dev runtime ready
|
||||
|
||||
:cleanup
|
||||
REM ── 5. Nettoyer les dossiers stagés ───────────────────────────
|
||||
echo [5/6] Cleaning up staged dirs...
|
||||
if exist "backend" rmdir /s /q "backend"
|
||||
if exist "frontend" rmdir /s /q "frontend"
|
||||
if exist "updater-off.json" del /q "updater-off.json"
|
||||
echo ✅ Cleaned
|
||||
|
||||
REM ── 6. Résultats ───────────────────────────────────────────────
|
||||
echo [6/6] Binaries generated:
|
||||
dir /b "%OUTDIR%\bundle\nsis\*.exe" 2>nul
|
||||
dir /b "%OUTDIR%\bundle\msi\*.msi" 2>nul
|
||||
echo.
|
||||
echo ✅ Done! Installers generated in: %OUTDIR%\bundle\
|
||||
endlocal
|
||||
@@ -0,0 +1,42 @@
|
||||
fn main() {
|
||||
// Version livrée : `VERSION` (racine du dépôt) est la source unique de
|
||||
// vérité (même fichier que backend/version.py et l'image Docker).
|
||||
// Repli : `git describe` quand le fichier n'est pas dans le contexte de build.
|
||||
let version = std::fs::read_to_string(
|
||||
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../VERSION"),
|
||||
)
|
||||
.ok()
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| {
|
||||
std::process::Command::new("git")
|
||||
.args(["describe", "--tags", "--dirty=-dirty"])
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.map(|s| s.trim().trim_start_matches('v').to_string())
|
||||
.unwrap_or_else(|| "0.0.0".to_string())
|
||||
});
|
||||
|
||||
// Extract short hash for display
|
||||
let short_hash = std::process::Command::new("git")
|
||||
.args(["rev-parse", "--short", "HEAD"])
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.map(|s| s.trim().to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
// SemVer base (strip -suffix)
|
||||
let semver_base = version.split('-').next().unwrap_or("0.0.0");
|
||||
|
||||
println!("cargo:rustc-env=GIT_VERSION={}", version);
|
||||
println!("cargo:rustc-env=GIT_HASH={}", short_hash);
|
||||
println!("cargo:rustc-env=SEMVER={}", semver_base);
|
||||
println!("cargo:rerun-if-changed=../VERSION");
|
||||
println!("cargo:rerun-if-changed=.git/HEAD");
|
||||
println!("cargo:rerun-if-changed=.git/refs/heads/main");
|
||||
println!("cargo:rerun-if-changed=.git/refs/tags");
|
||||
|
||||
tauri_build::build()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"identifier": "default",
|
||||
"description": "Default capabilities for ObsiGate Desktop",
|
||||
"windows": ["main"],
|
||||
"remote": {
|
||||
"urls": ["http://127.0.0.1:*", "http://localhost:*"]
|
||||
},
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"shell:allow-open",
|
||||
"shell:allow-execute",
|
||||
"dialog:default",
|
||||
"notification:default",
|
||||
"fs:default",
|
||||
"process:default",
|
||||
"store:default"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
{"default":{"identifier":"default","description":"Default capabilities for ObsiGate Desktop","remote":{"urls":["http://127.0.0.1:*","http://localhost:*"]},"local":true,"windows":["main"],"permissions":["core:default","shell:allow-open","shell:allow-execute","dialog:default","notification:default","fs:default","process:default","store:default"]}}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user