Files
wevia-brain/wevia-vault-context.php
Yanis Mahboub 197b8fa48b Wave 114 auto
2026-04-13 04:38:29 +02:00

46 lines
1.7 KiB
PHP

<?php
/**
* WEVIA Vault Context Loader
* Reduces LLM token usage by loading only relevant vault notes
* Include in master-api.php before LLM calls
*/
function wevia_vault_context($message, $maxNotes = 3) {
$VAULT = '/opt/obsidian-vault';
$ctx = '';
// 1. Always load critical doctrines (tiny, always relevant)
$critical = ['paradigme-ultime', 'anti-fragmentation', 'souverainete'];
foreach ($critical as $c) {
$f = "$VAULT/doctrines/$c.md";
if (file_exists($f)) {
$content = file_get_contents($f);
// Strip frontmatter
if (preg_match('/---.*?---\s*(.*)/s', $content, $m)) $content = $m[1];
$ctx .= trim(substr($content, 0, 200)) . "\n";
}
}
// 2. Keyword-based vault search (fast, no Qdrant needed)
$keywords = array_filter(explode(' ', strtolower($message)), fn($w) => strlen($w) >= 4);
if ($keywords) {
$found = 0;
$iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($VAULT));
foreach ($iter as $file) {
if ($file->getExtension() !== 'md' || $found >= $maxNotes) continue;
$content = file_get_contents($file->getPathname());
$lower = strtolower($content);
foreach ($keywords as $kw) {
if (stripos($lower, $kw) !== false) {
$rel = str_replace("$VAULT/", '', $file->getPathname());
if (preg_match('/---.*?---\s*(.*)/s', $content, $m)) $content = $m[1];
$ctx .= "[VAULT:$rel] " . trim(substr($content, 0, 300)) . "\n";
$found++;
break;
}
}
}
}
return $ctx ? "\n[VAULT CONTEXT]\n$ctx" : '';
}