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

155 lines
6.2 KiB
PHP

<?php
// WEVAL Arena Engine — Streaming + Consensus + Autonomous + Long-running
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
if ($_SERVER["REQUEST_METHOD"]==="OPTIONS") exit;
$raw = json_decode(file_get_contents("php://input"), true) ?: [];
$msg = $raw["message"] ?? "";
$mode = $raw["mode"] ?? "single"; // single|consensus|autonomous|stream
$models = $raw["models"] ?? ["auto"];
$duration = intval($raw["duration"] ?? 0); // minutes (0=immediate)
$task_id = $raw["task_id"] ?? "";
if (!$msg && !$task_id) { echo json_encode(["error" => "no message"]); exit; }
// === CONSENSUS MODE: Query 3+ providers, merge best ===
if ($mode === "consensus") {
$providers = ["nim-deepseek", "alibaba-qwen", "cf-deepseek-r1", "mistral", "or-gpt-oss"];
$results = [];
$env = @file_get_contents("/etc/weval/secrets.env") ?: "";
foreach (array_slice($providers, 0, 3) as $p) {
$ch = curl_init("https://weval-consulting.com/api/wevia-multi-provider.php");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(["message" => $msg, "model" => $p]),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_TIMEOUT => 30, CURLOPT_SSL_VERIFYPEER => false]);
$r = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code === 200) {
$d = json_decode($r, true);
if (!empty($d["content"])) {
$results[] = ["provider" => $d["provider"] ?? $p, "content" => $d["content"], "length" => strlen($d["content"])];
}
}
}
if ($results) {
// Pick best (longest) + add consensus summary
usort($results, fn($a, $b) => $b["length"] - $a["length"]);
$best = $results[0];
$summary = "CONSENSUS (" . count($results) . " IAs):\n";
foreach ($results as $i => $r) {
$summary .= ($i+1) . ". " . $r["provider"] . " (" . $r["length"] . "ch)\n";
}
echo json_encode([
"content" => $best["content"],
"consensus_summary" => $summary,
"provider" => "CONSENSUS " . count($results) . " IAs",
"participants" => array_map(fn($r) => $r["provider"], $results),
"model" => "multi-ai-consensus",
"cost" => "0EUR SOUVERAIN"
]);
exit;
}
}
// === AUTONOMOUS MODE: Background task, check status ===
if ($mode === "autonomous") {
$task_dir = "/tmp/arena-tasks/";
@mkdir($task_dir, 0777, true);
// Check existing task status
if ($task_id) {
$f = $task_dir . basename($task_id) . ".json";
if (file_exists($f)) {
echo file_get_contents($f);
} else {
echo json_encode(["status" => "pending", "task_id" => $task_id]);
}
exit;
}
// Create new autonomous task
$tid = "arena_" . time() . "_" . substr(md5($msg), 0, 6);
$task = [
"task_id" => $tid,
"status" => "running",
"message" => $msg,
"started" => date("Y-m-d H:i:s"),
"duration_min" => $duration,
"steps" => [],
];
// Execute immediately with Master
$ch = curl_init("https://weval-consulting.com/api/wevia-master-api.php");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(["message" => $msg]),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_TIMEOUT => 60, CURLOPT_SSL_VERIFYPEER => false]);
$r = curl_exec($ch);
$d = json_decode($r, true);
$master_result = $d["content"] ?? "";
$task["steps"][] = ["agent" => "WEVIA Master", "result" => substr($master_result, 0, 2000), "time" => date("H:i:s")];
$task["status"] = "completed";
$task["result"] = $master_result;
file_put_contents($task_dir . $tid . ".json", json_encode($task));
echo json_encode([
"content" => $master_result,
"task_id" => $tid,
"status" => "completed",
"provider" => "Arena Autonomous",
"model" => "master+multi-agent",
"cost" => "0EUR SOUVERAIN"
]);
exit;
}
// === STREAM MODE: SSE streaming (for long coding tasks) ===
if ($mode === "stream") {
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
// Step 1: Master analysis
echo "data: {\"step\":\"master\",\"status\":\"analyzing\"}\n\n";
ob_flush(); flush();
$ch = curl_init("https://weval-consulting.com/api/wevia-master-api.php");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(["message" => $msg]),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"], CURLOPT_TIMEOUT => 30]);
$master = json_decode(curl_exec($ch), true)["content"] ?? "";
echo "data: " . json_encode(["step" => "master", "content" => substr($master, 0, 500)]) . "\n\n";
ob_flush(); flush();
// Step 2: Best coder for implementation
echo "data: {\"step\":\"coding\",\"status\":\"generating\"}\n\n";
ob_flush(); flush();
$ch = curl_init("https://weval-consulting.com/api/wevia-multi-provider.php");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(["message" => $msg, "model" => "nim-deepseek"]),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"], CURLOPT_TIMEOUT => 60]);
$code_result = json_decode(curl_exec($ch), true)["content"] ?? "";
echo "data: " . json_encode(["step" => "coding", "content" => $code_result]) . "\n\n";
ob_flush(); flush();
echo "data: {\"step\":\"done\",\"status\":\"complete\"}\n\n";
echo "data: [DONE]\n\n";
exit;
}
// === SINGLE MODE: Direct to multi-provider ===
$model = $models[0] ?? "auto";
$ch = curl_init("https://weval-consulting.com/api/wevia-multi-provider.php");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(["message" => $msg, "model" => $model]),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"], CURLOPT_TIMEOUT => 30]);
$r = curl_exec($ch);
echo $r;
?>