Files
html/api/autolearn.php
2026-04-12 22:57:03 +02:00

58 lines
2.1 KiB
PHP

<?php
// WEVIA Auto-Learning API
// GET ?action=run — start full autolearn
// GET ?action=run&tests=greeting-fr,swot — run specific tests
// GET ?action=status — check status
// GET ?action=report — latest report
// GET ?action=learnings — all learnings
// GET ?action=patches — prompt improvement patches
header('Content-Type: application/json');
$action = $_GET['action'] ?? 'status';
$pidFile = '/tmp/autolearn.pid';
$logFile = '/tmp/autolearn.log';
$script = '/var/www/html/tests/wevia-autolearn.py';
$learnDir = '/var/www/html/test-report/autolearn';
switch ($action) {
case 'run':
if (file_exists($pidFile) && file_exists('/proc/' . trim(file_get_contents($pidFile)))) {
echo json_encode(['status' => 'running', 'log' => tail('/tmp/autolearn.log', 5)]);
exit;
}
$tests = $_GET['tests'] ?? '';
$args = $tests ? implode(' ', array_map('escapeshellarg', explode(',', $tests))) : '';
$pid = trim(shell_exec("nohup python3 $script $args > $logFile 2>&1 & echo $!"));
file_put_contents($pidFile, $pid);
echo json_encode(['status' => 'started', 'pid' => $pid]);
break;
case 'status':
$running = file_exists($pidFile) && file_exists('/proc/' . trim(@file_get_contents($pidFile)));
echo json_encode(['status' => $running ? 'running' : 'idle', 'log' => tail($logFile, 8)]);
break;
case 'report':
$f = "$learnDir/latest-report.json";
echo file_exists($f) ? file_get_contents($f) : json_encode(['error' => 'No report yet']);
break;
case 'learnings':
$f = "$learnDir/learnings.json";
echo file_exists($f) ? file_get_contents($f) : json_encode(['learnings' => []]);
break;
case 'patches':
$f = "$learnDir/prompt-patches.json";
echo file_exists($f) ? file_get_contents($f) : json_encode([]);
break;
default:
echo json_encode(['actions' => ['run','status','report','learnings','patches']]);
}
function tail($f, $n = 10) {
if (!file_exists($f)) return '';
return implode('', array_slice(file($f), -$n));
}