Files
wevia-brain/wevia-vault-context.php
Yanis Mahboub aa2e4657e5 Wave 114 auto
2026-04-13 13:37:52 +02:00

48 lines
1.7 KiB
PHP

<?php
// WEVIA Vault Context Loader — CACHED version (no recursive scan per request)
function wevia_vault_context($message, $maxNotes = 3) {
$VAULT = '/opt/obsidian-vault';
$cache = '/tmp/vault-context-cache.json';
// Use cache if < 5 min old
if (file_exists($cache) && (time() - filemtime($cache)) < 300) {
$index = json_decode(file_get_contents($cache), true) ?: [];
} else {
// Rebuild cache
$index = [];
foreach (['doctrines','infra','kb','decisions','arena','ethica'] as $dir) {
$d = "$VAULT/$dir";
if (!is_dir($d)) continue;
foreach (glob("$d/*.md") as $f) {
$content = file_get_contents($f);
if (preg_match('/---.*?---\s*(.*)/s', $content, $m)) $content = $m[1];
$rel = str_replace("$VAULT/", '', $f);
$index[$rel] = strtolower(substr($content, 0, 500));
}
}
@file_put_contents($cache, json_encode($index));
}
// Always inject 2 critical doctrines (tiny)
$ctx = '';
foreach (['doctrines/souverainete.md','doctrines/paradigme-ultime.md'] as $f) {
if (isset($index[$f])) $ctx .= substr($index[$f], 0, 150) . "\n";
}
// Keyword match from cache (no file I/O)
$words = array_filter(explode(' ', strtolower($message)), fn($w) => strlen($w) >= 4);
$found = 0;
foreach ($index as $file => $content) {
if ($found >= $maxNotes) break;
foreach ($words as $kw) {
if (strpos($content, $kw) !== false) {
$ctx .= "[VAULT:$file] " . substr($content, 0, 200) . "\n";
$found++;
break;
}
}
}
return $ctx ? "\n[VAULT CONTEXT]\n$ctx" : '';
}