79 lines
2.8 KiB
PHP
79 lines
2.8 KiB
PHP
<?php
|
|
// OPUS5 WEVIA MASTER DISPATCH PROXY - pre-dispatch stubs then proxy to master-api
|
|
// Workaround chattr+i on wevia-master-api.php
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }
|
|
|
|
$raw = file_get_contents('php://input');
|
|
$in = @json_decode($raw, true) ?: [];
|
|
$msg = trim($in['message'] ?? ($_POST['message'] ?? ''));
|
|
|
|
if (empty($msg)) {
|
|
http_response_code(200);
|
|
echo json_encode(['success'=>false, 'error'=>'message required']);
|
|
exit;
|
|
}
|
|
|
|
// === STEP 1: try match opus4 stub trigger ===
|
|
$msg_lower = mb_strtolower($msg);
|
|
$stubs = @glob('/var/www/html/api/wired-pending/intent-opus4-*.php') ?: [];
|
|
|
|
foreach ($stubs as $s) {
|
|
$info = @include $s;
|
|
if (!is_array($info) || empty($info['triggers'])) continue;
|
|
$status = $info['status'] ?? '';
|
|
if (!in_array($status, ['EXECUTED', 'PENDING_APPROVAL'])) continue;
|
|
|
|
foreach ($info['triggers'] as $trg) {
|
|
$trg = trim($trg);
|
|
if ($trg === '') continue;
|
|
if (stripos($msg_lower, mb_strtolower($trg)) !== false) {
|
|
$cmd = $info['cmd'] ?? '';
|
|
|
|
// Safety whitelist
|
|
$safe = false;
|
|
foreach (['/var/www/html/', 'echo ', 'curl ', 'php8.4 ', 'grep ', 'psql '] as $p) {
|
|
if (stripos($cmd, $p) === 0 || stripos($cmd, " $p") !== false) { $safe = true; break; }
|
|
}
|
|
if (!$safe) continue;
|
|
|
|
$start = microtime(true);
|
|
$out = @shell_exec('timeout 15 ' . $cmd . ' 2>&1');
|
|
$ms = round((microtime(true) - $start) * 1000);
|
|
|
|
@file_put_contents('/tmp/opus5-dispatcher.log', date('c') . " MATCH intent={$info['name']} trg=$trg ms=$ms\n", FILE_APPEND);
|
|
|
|
http_response_code(200);
|
|
echo json_encode([
|
|
'response' => "Intent '{$info['name']}' executed (trigger: $trg)\n" . trim((string)$out),
|
|
'executed' => true,
|
|
'provider' => 'opus5-dispatch-proxy',
|
|
'intent' => $info['name'],
|
|
'trigger_matched' => $trg,
|
|
'ms' => $ms,
|
|
'output' => trim((string)$out)
|
|
], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
// === STEP 2: no stub match → proxy to wevia-master-api ===
|
|
$ch = curl_init('http://127.0.0.1/api/wevia-master-api.php');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $raw,
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 30
|
|
]);
|
|
$resp = curl_exec($ch);
|
|
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
http_response_code(200);
|
|
echo $resp ?: json_encode(['success'=>false, 'error'=>'upstream_error', 'http'=>$http]);
|