Files
html/api/ambre-retry-wrap.php
2026-04-22 02:15:03 +02:00

110 lines
4.1 KiB
PHP

<?php
header("Content-Type: application/json");
$path = "/var/www/html/wevia.html";
$content = @file_get_contents($path);
$orig_size = strlen($content);
// Find all fallback fetch-sovereign calls
$changes = 0;
// Pattern: fetch('/api/sovereign/v1/chat/completions' or similar
// Wrap with retry: if first fetch fails with 503, retry after 2s, then after 5s (3 tries total)
// Simpler approach: intercept fetch to /api/sovereign via monkey patch at the TOP of the big script
// Insert a tiny retry wrapper right after <script> opens (line 718)
$insert_after = "<script>\n"; // Find the big script
$big_script_start = strpos($content, "<script>", strpos($content, "a11y-auto.js")); // the big one starts after a11y
if ($big_script_start === false) {
echo json_encode(["error"=>"big script not found"]);
exit;
}
// Check if retry wrapper already injected
if (strpos($content, "/* AMBRE-SOVEREIGN-RETRY */") !== false) {
echo json_encode(["skip"=>"already injected", "orig"=>$orig_size]);
exit;
}
$retry_wrapper = <<<'JS'
<script>
/* AMBRE-SOVEREIGN-RETRY · auto-retry 503/429/502 on /api/sovereign + cascade */
(function(){
if (window.__retryFetchInstalled) return;
window.__retryFetchInstalled = true;
var _origFetch = window.fetch;
window.fetch = function(url, opts) {
var u = typeof url === 'string' ? url : (url && url.url) || '';
var shouldRetry = u.indexOf('/api/sovereign') >= 0 || u.indexOf('chat/completions') >= 0 || u.indexOf('/api/ambre') >= 0 || u.indexOf('/api/wevia-') >= 0;
if (!shouldRetry) return _origFetch.apply(this, arguments);
var maxRetry = 3;
var backoff = [0, 1500, 4000];
var self = this;
var args = arguments;
return new Promise(function(resolve, reject) {
function tryOnce(attempt) {
var delay = backoff[attempt] || 0;
setTimeout(function() {
_origFetch.apply(self, args).then(function(r) {
if ((r.status === 503 || r.status === 429 || r.status === 502) && attempt < maxRetry - 1) {
console.warn('[retry] ' + u + ' got ' + r.status + ', retry #' + (attempt+1));
tryOnce(attempt + 1);
} else {
resolve(r);
}
}).catch(function(err) {
if (attempt < maxRetry - 1) {
console.warn('[retry] ' + u + ' err, retry #' + (attempt+1) + ': ' + err.message);
tryOnce(attempt + 1);
} else {
reject(err);
}
});
}, delay);
}
tryOnce(0);
});
};
})();
</script>
<script>
JS;
$content_new = str_replace($insert_after, $retry_wrapper, $content, $count);
if ($count === 0) {
echo json_encode(["error"=>"anchor not found"]);
exit;
}
// But we'd replace ALL <script>. We only want the first after a11y
// Instead find the specific position
$content = @file_get_contents($path);
// Find position of big inline <script> (after 'a11y-auto.js')
$a11y_pos = strpos($content, "a11y-auto.js");
$big_script_pos = strpos($content, "<script>", $a11y_pos);
if ($big_script_pos === false) {
echo json_encode(["error"=>"big script not found"]);
exit;
}
// Insert retry wrapper BEFORE the big <script>
$inject = str_replace("<script>", "", $retry_wrapper); // retry_wrapper already has opening <script>
// Actually simpler: insert entire retry_wrapper RIGHT BEFORE big <script>
// retry_wrapper ends with <script> so it opens the next block seamlessly
$content_new = substr($content, 0, $big_script_pos) . $retry_wrapper . substr($content, $big_script_pos + 8); // skip the original "<script>"
$backup = "/opt/wevads/vault/wevia.html.GOLD-" . date("Ymd-His") . "-retry-wrapper";
@copy($path, $backup);
$wrote = @file_put_contents($path, $content_new);
echo json_encode([
"orig" => $orig_size,
"new" => strlen($content_new),
"delta" => strlen($content_new) - $orig_size,
"wrote" => $wrote,
"backup" => basename($backup),
]);