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

34 lines
1.2 KiB
PHP

<?php
header("Content-Type: application/json");
$q = $_POST["intent"] ?? $_GET["intent"] ?? "";
$pattern = $_POST["pattern"] ?? $_GET["pattern"] ?? "";
$engine = $_POST["engine"] ?? $_GET["engine"] ?? "Cerebras";
$response = $_POST["response"] ?? $_GET["response"] ?? "";
if(!$q || !$pattern) die(json_encode(["error"=>"need intent + pattern"]));
$file = "/var/www/html/api/wevia-smart-router.php";
$content = file_get_contents($file);
// Check if intent already exists
if(strpos($content, $q) !== false) {
die(json_encode(["status"=>"exists","intent"=>$q]));
}
// Add new intent before "return null"
$newline = " if(preg_match('/$pattern/',\\$q)) return['response'=>'$response','engine'=>'$engine','intent'=>'$q'];\n";
$content = str_replace(" return null;", $newline." return null;", $content);
// Backup + write
file_put_contents($file.".bak", file_get_contents($file));
file_put_contents($file, $content);
// Verify
$lint = trim(shell_exec("php -l $file 2>&1"));
if(strpos($lint, "No syntax errors") === false) {
// Rollback
copy($file.".bak", $file);
die(json_encode(["error"=>"PHP syntax error, rolled back","lint"=>$lint]));
}
echo json_encode(["status"=>"wired","intent"=>$q,"pattern"=>$pattern,"engine"=>$engine]);