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

46 lines
1.4 KiB
PHP

<?php
// WEVIA Public SSE Streaming — Long responses like WEVIA Master
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
header("Access-Control-Allow-Origin: *");
if ($_SERVER["REQUEST_METHOD"]==="OPTIONS"){exit;}
set_time_limit(60);
$input = json_decode(file_get_contents("php://input"), true) ?: [];
$msg = $input["message"] ?? "";
if (!$msg) { echo "data: " . json_encode(["type"=>"error","content"=>"No message"]) . "\n\n"; exit; }
function sse_send($type, $content) {
echo "data: " . json_encode(["type"=>$type,"content"=>$content], JSON_UNESCAPED_UNICODE) . "\n\n";
ob_flush(); flush();
}
sse_send("thinking", "Analyse en cours...");
// Call fast engine
$ch = curl_init("http://127.0.0.1/api/weval-ia-fast.php");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(["message"=>$msg,"mode"=>"fast"]),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$result = curl_exec($ch);
curl_close($ch);
$d = json_decode($result, true);
$resp = $d["response"] ?? "Service en maintenance";
$prov = $d["provider"] ?? "WEVIA";
// Stream response in chunks (simulate typing)
$chunks = str_split($resp, 60);
foreach ($chunks as $i => $chunk) {
sse_send("token", $chunk);
usleep(20000); // 20ms between chunks
}
sse_send("provider", $prov);
sse_send("done", "");