106 lines
4.3 KiB
PHP
106 lines
4.3 KiB
PHP
<?php
|
|
// V9.48 Token Health REAL · doctrine #4 honest probe
|
|
// V113: Added 5-min file cache to prevent self-inflicted provider rate-limits
|
|
header("Content-Type: application/json");
|
|
set_time_limit(60);
|
|
|
|
$cache_file = "/tmp/token-health-cache.json";
|
|
$cache_ttl = 300; // 5 minutes
|
|
$force = !empty($_GET["force"]) || !empty($_POST["force"]);
|
|
|
|
// Try cache first (unless force)
|
|
if (!$force && is_readable($cache_file)) {
|
|
$mtime = filemtime($cache_file);
|
|
$age = time() - $mtime;
|
|
if ($age < $cache_ttl) {
|
|
$cached = @json_decode(@file_get_contents($cache_file), true);
|
|
if (is_array($cached)) {
|
|
$cached["cache_hit"] = true;
|
|
$cached["cache_age_sec"] = $age;
|
|
echo json_encode($cached, JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
$secrets_file = "/etc/weval/secrets.env";
|
|
$env = [];
|
|
if (is_readable($secrets_file)) {
|
|
$lines = file($secrets_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if ($line === "" || $line[0] === "#") continue;
|
|
if (strpos($line, "=") === false) continue;
|
|
list($k, $v) = explode("=", $line, 2);
|
|
$env[trim($k)] = trim($v, "\"' \t");
|
|
}
|
|
}
|
|
|
|
$providers = [
|
|
["name"=>"sambanova", "env"=>"SAMBANOVA_KEY", "url"=>"https://api.sambanova.ai/v1/models", "auth"=>"bearer"],
|
|
["name"=>"openrouter", "env"=>"OPENROUTER_KEY", "url"=>"https://openrouter.ai/api/v1/models", "auth"=>"bearer"],
|
|
["name"=>"cerebras", "env"=>"CEREBRAS_API_KEY", "url"=>"https://api.cerebras.ai/v1/models", "auth"=>"bearer"],
|
|
["name"=>"groq", "env"=>"GROQ_KEY", "url"=>"https://api.groq.com/openai/v1/models", "auth"=>"bearer"],
|
|
["name"=>"gemini", "env"=>"GEMINI_KEY", "url"=>"https://generativelanguage.googleapis.com/v1beta/models", "auth"=>"query"],
|
|
["name"=>"mistral", "env"=>"MISTRAL_KEY", "url"=>"https://api.mistral.ai/v1/models", "auth"=>"bearer"],
|
|
["name"=>"deepseek", "env"=>"DEEPSEEK_KEY", "url"=>"https://api.deepseek.com/v1/models", "auth"=>"bearer"],
|
|
["name"=>"huggingface", "env"=>"HF_TOKEN", "url"=>"https://huggingface.co/api/whoami-v2", "auth"=>"bearer"],
|
|
["name"=>"alibaba", "env"=>"ALIBABA_KEY", "url"=>"https://dashscope.aliyuncs.com/compatible-mode/v1/models", "auth"=>"bearer"],
|
|
["name"=>"anthropic", "env"=>"ANTHROPIC_KEY", "url"=>"https://api.anthropic.com/v1/models", "auth"=>"anthropic"],
|
|
["name"=>"github", "env"=>"GITHUB_TOKEN", "url"=>"https://api.github.com/user", "auth"=>"bearer"]
|
|
];
|
|
|
|
$results = []; $ok = 0; $ko = 0;
|
|
foreach ($providers as $p) {
|
|
$key = $env[$p["env"]] ?? "";
|
|
if (empty($key)) {
|
|
$results[] = ["provider"=>$p["name"], "status"=>"NO_KEY", "http_code"=>0];
|
|
$ko++; continue;
|
|
}
|
|
$ch = curl_init();
|
|
$url = $p["url"];
|
|
if ($p["auth"] === "query") $url .= "?key=" . urlencode($key);
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
$h = [];
|
|
if ($p["auth"] === "bearer") $h[] = "Authorization: Bearer $key";
|
|
if ($p["auth"] === "anthropic") { $h[] = "x-api-key: $key"; $h[] = "anthropic-version: 2023-06-01"; }
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $h);
|
|
curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
$isok = ($code >= 200 && $code < 300);
|
|
$results[] = [
|
|
"provider" => $p["name"],
|
|
"status" => $isok ? "OK" : "EXPIRED",
|
|
"http_code" => $code,
|
|
"key_prefix" => substr($key, 0, 10) . "..." . substr($key, -4),
|
|
"key_length" => strlen($key)
|
|
];
|
|
if ($isok) $ok++; else $ko++;
|
|
}
|
|
|
|
$response = [
|
|
"ok" => true,
|
|
"ts" => date("c"),
|
|
"version" => "v9.48-honest-token-probe-raw-parse+v113-cache5min",
|
|
"doctrine" => "#4 honest real API probe + self-rate-limit respect",
|
|
"cache_hit" => false,
|
|
"cache_ttl_sec" => $cache_ttl,
|
|
"env_keys_loaded" => count($env),
|
|
"summary" => [
|
|
"total" => count($providers),
|
|
"live_ok" => $ok,
|
|
"expired_ko" => $ko,
|
|
"health_pct" => count($providers) > 0 ? round(($ok / count($providers)) * 100, 1) : 0
|
|
],
|
|
"results" => $results
|
|
];
|
|
|
|
// Write cache (best-effort, non-fatal)
|
|
@file_put_contents($cache_file, json_encode($response, JSON_PRETTY_PRINT));
|
|
|
|
echo json_encode($response, JSON_PRETTY_PRINT);
|