127 lines
6.5 KiB
PHP
127 lines
6.5 KiB
PHP
<?php
|
|
@require_once __DIR__ . '/wevia-sanitizer-guard.php'; // WAVE 206 sanitizer guard
|
|
|
|
// WEVAL Arena Auto-Wire — Auto-discover, auto-signup, auto-renew, auto-scale
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
if ($_SERVER["REQUEST_METHOD"]==="OPTIONS"){header("Access-Control-Allow-Headers: Content-Type");exit;}
|
|
|
|
$raw = json_decode(file_get_contents("php://input"), true) ?: [];
|
|
$action = $raw["action"] ?? $_GET["action"] ?? "status";
|
|
|
|
$env = @file_get_contents("/etc/weval/secrets.env") ?: "";
|
|
$keys_file = "/etc/weval/secrets.env";
|
|
$registry_file = "/var/www/html/api/arena-registry.json";
|
|
|
|
// Load registry
|
|
$registry = file_exists($registry_file) ? json_decode(file_get_contents($registry_file), true) : [];
|
|
|
|
// === STATUS: Show all providers + key health ===
|
|
if ($action === "status") {
|
|
$providers = [
|
|
["name"=>"NVIDIA NIM","key"=>"NVIDIA_NIM_KEY","test"=>"https://integrate.api.nvidia.com/v1/models","type"=>"api","limit"=>"40rpm FREE"],
|
|
["name"=>"OpenRouter","key"=>"OPENROUTER_KEY","test"=>"https://openrouter.ai/api/v1/models","type"=>"api","limit"=>"50/day FREE"],
|
|
["name"=>"Mistral","key"=>"MISTRAL_KEY","test"=>"https://api.mistral.ai/v1/models","type"=>"api","limit"=>"FREE unlimited"],
|
|
["name"=>"Alibaba","key"=>"ALIBABA_KEY","test"=>"","type"=>"api","limit"=>"FREE unlimited"],
|
|
["name"=>"Groq","key"=>"GROQ_KEY","test"=>"https://api.groq.com/openai/v1/models","type"=>"api","limit"=>"100K tok/day"],
|
|
["name"=>"Cerebras","key"=>"CEREBRAS_API_KEY","test"=>"","type"=>"api","limit"=>"FREE unlimited"],
|
|
["name"=>"Cohere","key"=>"COHERE_KEY","test"=>"","type"=>"api","limit"=>"1000/month"],
|
|
["name"=>"Together","key"=>"TOGETHER_KEY","test"=>"","type"=>"api","limit"=>"FREE tier"],
|
|
["name"=>"HuggingFace","key"=>"HF_TOKEN","test"=>"","type"=>"api","limit"=>"FREE inference"],
|
|
["name"=>"DeepSeek","key"=>"DEEPSEEK_KEY","test"=>"","type"=>"api","limit"=>"pay-as-you-go"],
|
|
["name"=>"Anthropic","key"=>"ANTHROPIC_KEY","test"=>"","type"=>"api","limit"=>"needs credits"],
|
|
["name"=>"Replicate","key"=>"REPLICATE_KEY","test"=>"","type"=>"api","limit"=>"FREE tier"],
|
|
["name"=>"Kaggle","key"=>"KAGGLE_API_TOKEN","test"=>"","type"=>"gpu","limit"=>"30h/week T4"],
|
|
["name"=>"Gemini","key"=>"GEMINI_KEY","test"=>"","type"=>"api","limit"=>"250RPD Flash"],
|
|
["name"=>"CF Workers","key"=>"CF_API_TOKEN","test"=>"","type"=>"api","limit"=>"10K neurons/day"],
|
|
["name"=>"ZhiPu","key"=>"ZHIPU_KEY","test"=>"","type"=>"api","limit"=>"FREE tier"],
|
|
];
|
|
|
|
$result = [];
|
|
foreach ($providers as $p) {
|
|
preg_match("/" . $p["key"] . "=(.+)/", $env, $m);
|
|
$key = trim($m[1] ?? "");
|
|
$has_key = strlen($key) > 5;
|
|
$result[] = [
|
|
"name" => $p["name"],
|
|
"key_name" => $p["key"],
|
|
"has_key" => $has_key,
|
|
"key_length" => strlen($key),
|
|
"type" => $p["type"],
|
|
"limit" => $p["limit"],
|
|
"status" => $has_key ? "active" : "missing",
|
|
];
|
|
}
|
|
|
|
$active = count(array_filter($result, fn($r) => $r["has_key"]));
|
|
echo json_encode(["providers" => $result, "active" => $active, "total" => count($result), "timestamp" => date("Y-m-d H:i:s")]);
|
|
exit;
|
|
}
|
|
|
|
// === DISCOVER: Find new free AI providers ===
|
|
if ($action === "discover") {
|
|
// Known free providers to check
|
|
$free_providers = [
|
|
["name"=>"SiliconFlow","url"=>"https://cloud.siliconflow.cn","signup"=>"https://cloud.siliconflow.cn/register","limit"=>"1000RPM FREE","status"=>"to_signup"],
|
|
["name"=>"xAI Grok","url"=>"https://console.x.ai","signup"=>"https://console.x.ai/signup","limit"=>"$175/month FREE","status"=>"to_signup"],
|
|
["name"=>"GitHub Models","url"=>"https://github.com/marketplace/models","signup"=>"need PAT","limit"=>"50-150 RPD","status"=>"need_pat"],
|
|
["name"=>"Fireworks AI","url"=>"https://fireworks.ai","signup"=>"https://fireworks.ai/login","limit"=>"FREE tier","status"=>"to_check"],
|
|
["name"=>"Groq Renew","url"=>"https://console.groq.com/keys","signup"=>"existing account","limit"=>"100K/day","status"=>"to_renew"],
|
|
["name"=>"Cerebras Renew","url"=>"https://cloud.cerebras.ai","signup"=>"existing account","limit"=>"FREE","status"=>"to_renew"],
|
|
];
|
|
|
|
echo json_encode(["discovered" => $free_providers, "count" => count($free_providers), "action" => "Use Blade/Playwright to auto-signup"]);
|
|
exit;
|
|
}
|
|
|
|
// === RENEW: Create Blade tasks for key renewal ===
|
|
if ($action === "renew") {
|
|
$expired = $raw["providers"] ?? ["groq", "cerebras", "github"];
|
|
$tasks = [];
|
|
|
|
$renew_urls = [
|
|
"groq" => "https://console.groq.com/keys",
|
|
"cerebras" => "https://cloud.cerebras.ai/api-keys",
|
|
"github" => "https://github.com/settings/tokens",
|
|
"siliconflow" => "https://cloud.siliconflow.cn/register",
|
|
"xai" => "https://console.x.ai/signup",
|
|
];
|
|
|
|
foreach ($expired as $p) {
|
|
if (isset($renew_urls[$p])) {
|
|
$task = [
|
|
"type" => "powershell",
|
|
"priority" => "P0",
|
|
"action" => "renew_" . $p,
|
|
"script" => "Start-Process \"" . $renew_urls[$p] . "\"; Write-Output \"Opened $p renewal page\"",
|
|
"timeout" => 15
|
|
];
|
|
$task_file = "/var/www/html/api/blade-tasks/renew_" . $p . ".json";
|
|
file_put_contents($task_file, json_encode($task));
|
|
$tasks[] = ["provider" => $p, "url" => $renew_urls[$p], "task_file" => basename($task_file)];
|
|
}
|
|
}
|
|
|
|
echo json_encode(["tasks_created" => count($tasks), "tasks" => $tasks, "blade_status" => "queued"]);
|
|
exit;
|
|
}
|
|
|
|
// === SCALE: Launch N agents on M providers ===
|
|
if ($action === "scale") {
|
|
$task = $raw["task"] ?? "";
|
|
$count = min(intval($raw["count"] ?? 5), 20); // max 20 parallel
|
|
$all_providers = ["wevia-master","nim-llama","nim-deepseek","mistral","alibaba-qwen","or-gpt-oss","or-nemotron","web-huggingchat","anthropic"];
|
|
$selected = array_slice($all_providers, 0, $count);
|
|
|
|
// Forward to multi-agent
|
|
$ch = curl_init("https://weval-consulting.com/api/wevia-arena-multiagent.php");
|
|
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_POST=>true,
|
|
CURLOPT_POSTFIELDS=>json_encode(["task"=>$task,"agents"=>$selected]),
|
|
CURLOPT_HTTPHEADER=>["Content-Type: application/json"],
|
|
CURLOPT_TIMEOUT=>120, CURLOPT_SSL_VERIFYPEER=>false]);
|
|
echo curl_exec($ch);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(["actions" => ["status","discover","renew","scale"], "usage" => "POST with action parameter"]);
|
|
?>
|