Files
html/api/opus5-autonomous-orchestrator.php

124 lines
5.3 KiB
PHP

<?php
// OPUS5 — WEVIA AUTONOMOUS META-ORCHESTRATOR v2
// AMÉLIORATIONS v2:
// - Parallelize chaine cognitive (curl_multi)
// - Async par défaut pour scripts lourds (speculative, cot_tree, reflect, dialectical)
// - Classification rapide (no regex costly)
// - Budget: 10s dispatch + 2s memory/sentiment sync + async for heavy
header('Content-Type: application/json');
$t0 = microtime(true);
$R = ['ts'=>date('c'), 'source'=>'opus5-autonomous-orchestrator-v2'];
$raw = file_get_contents('php://input');
$d = json_decode($raw, true) ?: [];
$msg = (string)($d['message'] ?? '');
$session = (string)($d['session'] ?? 'auto-' . bin2hex(random_bytes(4)));
if (!$msg) {
http_response_code(400);
echo json_encode(['err'=>'missing_message']);
exit;
}
// === CLASSIFICATION RAPIDE ===
$words = str_word_count($msg);
$ml = strtolower($msg);
$has_multi = preg_match('/\b(puis|ensuite|apres|parallele)\b/u', $ml);
$has_analysis = preg_match('/\b(analys|explique|pourquoi|detail|approfond)\b/u', $ml);
$has_memory = preg_match('/\b(souvien|rappell|dernier|histori)\b/u', $ml);
$complexity = 'simple';
if ($has_multi) $complexity = 'multi-step';
elseif ($has_analysis || $words > 20) $complexity = 'deep';
elseif ($has_memory) $complexity = 'medium';
$R['classification'] = ['words'=>$words, 'complexity'=>$complexity];
// === LAUNCH ASYNC BACKGROUND (fire-and-forget) ===
// Ces scripts tournent en background, log dans /var/log/weval
$async_tasks = [];
if ($complexity === 'multi-step') {
$async_tasks[] = ['name'=>'dialectical', 'cmd'=>"/opt/weval-ops/top-ia/dialectical.sh " . escapeshellarg(substr($msg,0,500))];
$async_tasks[] = ['name'=>'self_consistency', 'cmd'=>"/opt/weval-ops/top-ia/self_consistency.sh " . escapeshellarg(substr($msg,0,500))];
} elseif ($complexity === 'deep') {
$async_tasks[] = ['name'=>'cot_tree', 'cmd'=>"/opt/weval-ops/top-ia/cot_tree.sh " . escapeshellarg(substr($msg,0,500))];
$async_tasks[] = ['name'=>'reflect_loop', 'cmd'=>"/opt/weval-ops/top-ia/reflect_loop.sh " . escapeshellarg(substr($msg,0,500))];
}
// Post-hooks toujours async
$async_tasks[] = ['name'=>'meta_cognition', 'cmd'=>"/opt/weval-ops/top-ia/meta_cognition_hook.sh " . escapeshellarg($session) . " post"];
$async_tasks[] = ['name'=>'memory_store', 'cmd'=>"/opt/weval-ops/top-ia/memory_store.sh " . escapeshellarg($session) . " " . escapeshellarg(substr($msg,0,200))];
$logf = "/var/log/weval/orch-" . $session . ".log";
foreach ($async_tasks as $t) {
@shell_exec("timeout 30 " . $t['cmd'] . " >> $logf 2>&1 &");
}
$R['async_launched'] = array_column($async_tasks, 'name');
// === SYNC CRITICAL PATH (memory_recall + sentiment, budget 2s) ===
$sync_results = [];
if ($complexity !== 'simple') {
$t_mem = microtime(true);
$mem = @shell_exec("timeout 1 /opt/weval-ops/top-ia/memory_recall.sh " . escapeshellarg($session) . " " . escapeshellarg(substr($msg,0,120)) . " 2>&1");
$sync_results[] = ['step'=>'memory_recall', 'ms'=>round((microtime(true)-$t_mem)*1000), 'preview'=>substr((string)$mem,0,150)];
}
// === DISPATCH PRINCIPAL (budget 10s) ===
$t_disp = microtime(true);
$ch = curl_init('https://127.0.0.1/api/wevia-master-dispatch.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['message'=>$msg, 'session'=>$session]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json','Host: weval-consulting.com'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 2
]);
$resp = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$sync_results[] = ['step'=>'dispatch_proxy', 'http'=>$http, 'ms'=>round((microtime(true)-$t_disp)*1000)];
// Parse dispatch response
$parsed = @json_decode((string)$resp, true);
$final_response = '';
$final_provider = '';
if ($parsed) {
$final_response = $parsed['response'] ?? $parsed['content'] ?? $parsed['output'] ?? '';
$final_provider = $parsed['provider'] ?? 'dispatch-proxy';
}
// === FALLBACK si dispatch failed ou empty ===
if (!$final_response || strlen($final_response) < 10) {
// Try opus5-weval-ia-fast-safe (intercepte prospect/ethica)
$ch = curl_init('https://127.0.0.1/api/opus5-weval-ia-fast-safe.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['message'=>$msg]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json','Host: weval-consulting.com'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_TIMEOUT => 8
]);
$fb = curl_exec($ch);
curl_close($ch);
$fbp = @json_decode((string)$fb, true);
if ($fbp && isset($fbp['response'])) {
$final_response = $fbp['response'];
$final_provider = ($fbp['provider'] ?? 'safe-wrapper') . '-fallback';
$sync_results[] = ['step'=>'fallback_safe_wrapper', 'used'=>true];
}
}
$R['sync_pipeline'] = $sync_results;
$R['final_response'] = $final_response ?: '[orchestrator] Aucune réponse du pipeline.';
$R['final_provider'] = $final_provider;
$R['total_ms'] = round((microtime(true)-$t0)*1000);
$R['session'] = $session;
$R['doctrine'] = '67 v2 — meta-orchestrator parallel (async heavy, sync critical path, fallback safe wrapper)';
echo json_encode($R, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);