Files
html/api/wevia-doctrine-74-fix-intent.php
2026-04-17 18:25:01 +02:00

125 lines
6.0 KiB
PHP

<?php
// Intent doctrine_74_fix - WEVIA auto-rewrite legacy plans/docs retirant patterns "Yacine manuel"
// Doctrine 74 compliant: chat NL → WEVIA rewrite legacy avec references intents WEVIA
// Mode dry-run par défaut (retourne diff), mode apply si message contient "apply"
if (!function_exists('wevia_doctrine_74_fix')) {
function wevia_doctrine_74_fix($msg) {
if (!$msg) return false;
if (!preg_match('/\b(doctrine\s*74\s*fix|fix\s*manu[ea]l|purge\s*manu[ea]l|rewrite\s*legacy|clean\s*violations)\b/i', $msg)) return false;
$apply_mode = (bool)preg_match('/\b(apply|execute|confirm)\b/i', $msg);
$out = ['intent' => 'doctrine_74_fix', 'tool' => 'legacy_manual_rewrite', 'mode' => $apply_mode ? 'APPLY' : 'DRY_RUN'];
// Replacements : match common patterns + leur alternatives doctrine-74 compliant
$replacements = [
// Actions portails
'/Yacine\s+doit\s+ouvrir\s+(le\s+)?portail\s+(\w+)/i' => 'WEVIA chat "blade_open_$2" → Blade Selenium ouvre $2',
'/Action\s+physique\s*:\s*portail\s+(\w+)/i' => 'Intent WEVIA `$1_reregister` wire Blade Selenium',
// Clicks / envois
'/Yacine\s+doit\s+cliquer\s+(sur\s+)?Send/i' => 'WEVIA chat "send" → Selenium Gmail clique Send',
'/Yacine\s+clique\s+manuellement/i' => 'WEVIA chat confirme → Blade Selenium exec',
'/clic\s+manuel\s+Yacine/i' => 'chat NL WEVIA confirm',
// Backlog humains
'/Backlog\s+manuel\s*:\s*/i' => 'Backlog intent WEVIA à créer : ',
'/backlog\s+humain/i' => 'backlog intents WEVIA',
'/action\s+Yacine\s+manuelle/i' => 'chat NL WEVIA → intent',
'/Yacine\s+(30sec\s+)?clic\s*x?\s*\d*/i' => 'WEVIA chat "go" → Selenium',
// Commandes terminal
'/Yacine\s+doit\s+taper\s+(la\s+)?commande/i' => 'WEVIA intent exec via sentinel',
'/copier.*commande.*(exec|exécuter).*terminal/i' => 'WEVIA intent exec via sentinel/CX',
// SMS / OVH / Azure
'/manipulation\s+SMS/i' => 'WEVIA intent sms_fetch via Blade/Twilio',
'/manipulation\s+portail/i' => 'WEVIA intent blade_open + Selenium',
'/OVH\s+portail\s+à\s+ouvrir\s+par\s+Yacine/i' => 'Intent WEVIA `ovh_action` wire Blade Selenium',
'/Azure\s+portail\s+Yacine/i' => 'Intent WEVIA `azure_reregister` wire Blade Selenium',
];
$wiki_files = glob('/opt/weval-l99/wiki/PLAN-ACTION-V*.md');
$vault_file = '/opt/wevads/vault/doctrines.md';
$files_to_scan = array_merge($wiki_files, [$vault_file]);
$changes = [];
$total_replacements = 0;
foreach ($files_to_scan as $f) {
if (!is_readable($f)) continue;
$content = file_get_contents($f);
$original = $content;
$file_changes = [];
foreach ($replacements as $pattern => $replace) {
$matches = [];
if (preg_match_all($pattern, $content, $matches)) {
$new_content = preg_replace($pattern, $replace, $content);
if ($new_content !== $content) {
$file_changes[] = [
'pattern' => substr($pattern, 1, 60),
'count' => count($matches[0]),
'sample_before' => substr($matches[0][0] ?? '', 0, 80),
'sample_after' => substr(preg_replace($pattern, $replace, $matches[0][0] ?? ''), 0, 80),
];
$total_replacements += count($matches[0]);
$content = $new_content;
}
}
}
if ($file_changes) {
$changes[basename($f)] = [
'changes_count' => array_sum(array_column($file_changes, 'count')),
'changes' => $file_changes,
];
// Apply if mode
if ($apply_mode && $content !== $original) {
// Backup GOLD first (doctrine 3)
$gold = '/opt/wevads/vault/' . basename($f) . '.GOLD-pre-d74-fix-' . date('Ymd-His');
@copy($f, $gold);
// Use sudo via shell (www-data has NOPASSWD)
$tmp = '/tmp/d74_rewrite_' . md5($f) . '.md';
file_put_contents($tmp, $content);
exec("sudo -n cp " . escapeshellarg($tmp) . " " . escapeshellarg($f) . " 2>&1", $cp_out, $rc_cp);
@unlink($tmp);
$changes[basename($f)]['applied'] = ($rc_cp === 0);
$changes[basename($f)]['gold_backup'] = $gold;
}
}
}
$out['files_scanned'] = count($files_to_scan);
$out['files_changed'] = count($changes);
$out['total_replacements'] = $total_replacements;
$out['changes'] = $changes;
if (!$apply_mode) {
$out['next_step'] = 'Yacine chat "doctrine 74 fix apply" pour appliquer les changements (avec GOLD backup automatique)';
} else {
$out['applied'] = true;
$out['validation'] = 'Run `audit manuel` to verify compliance improved';
}
$out['doctrine'] = 'Doctrine 74 auto-fix: patterns "Yacine manuel" → references intents WEVIA';
header("Content-Type: application/json");
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
exit;
}
}
$_d74f_msg = '';
$_body = @file_get_contents('php://input');
if ($_body) {
$_j = @json_decode($_body, true);
if (is_array($_j) && !empty($_j['message'])) $_d74f_msg = $_j['message'];
}
if (!$_d74f_msg) $_d74f_msg = $_POST['message'] ?? $_GET['message'] ?? '';
if ($_d74f_msg) wevia_doctrine_74_fix($_d74f_msg);