fix(ai): cap request timeout

This commit is contained in:
Guillaume ARM 2026-06-09 08:33:24 +02:00
parent f6d6c9b5af
commit ccd3dfef7b
5 changed files with 25 additions and 23 deletions

View File

@ -1,6 +1,7 @@
local PING_PROMPT = 'reply with exactly: pong';
local DEFAULT_TIMEOUT_SECONDS = 1200;
local DEFAULT_TIMEOUT_SECONDS = 60;
local MAX_TIMEOUT_SECONDS = 60;
local DEFAULT_LUA_EXEC_MAX_RETRIES = 2;
local DEFAULT_LUA_EXEC_TIMEOUT_SECONDS = 5;
@ -152,8 +153,9 @@ local function createAi(opts)
local raw = options.timeoutSeconds;
if raw == nil then raw = settingsLib.get('opencc.timeout_seconds'); end
local n = tonumber(raw);
if n and n > 0 then return n; end
return DEFAULT_TIMEOUT_SECONDS;
if not n or n <= 0 then n = DEFAULT_TIMEOUT_SECONDS; end
if n > MAX_TIMEOUT_SECONDS then n = MAX_TIMEOUT_SECONDS; end
return n;
end
local function resolveLuaExecMaxRetries(options)
@ -195,35 +197,35 @@ local function createAi(opts)
return headers;
end
local function doGet(cfg, path)
local response, _, errorResponse = httpLib.get({
url = cfg.url .. path,
headers = buildHeaders(cfg),
timeout = cfg.timeoutSeconds,
});
local function callHttp(method, request)
local ok, response, httpErr, errorResponse = pcall(httpLib[method], request);
if not ok then
return nil, 'http ' .. method .. ' threw: ' .. tostring(response);
end
response = response or errorResponse;
if not response then
return nil, 'serveur injoignable';
return nil, 'serveur injoignable: ' .. tostring(httpErr or 'unknown error');
end
local code = statusCode(response);
local body = readAllAndClose(response);
return body, code;
end
local function doGet(cfg, path)
return callHttp('get', {
url = cfg.url .. path,
headers = buildHeaders(cfg),
timeout = cfg.timeoutSeconds,
});
end
local function doPost(cfg, path, payload)
local response, _, errorResponse = httpLib.post({
return callHttp('post', {
url = cfg.url .. path,
body = textutils.serializeJSON(payload),
headers = buildHeaders(cfg),
timeout = cfg.timeoutSeconds,
});
response = response or errorResponse;
if not response then
return nil, 'serveur injoignable';
end
local code = statusCode(response);
local body = readAllAndClose(response);
return body, code;
end
function api.clearSession()

View File

@ -1,6 +1,6 @@
{
"name": "TrapOS",
"version": "0.6.1",
"version": "0.6.2",
"branch": "next",
"packages": [
"trapos"

View File

@ -5,7 +5,7 @@
"trapos-boot": "0.2.2",
"trapos-net": "0.2.1",
"trapos-ui": "0.2.2",
"trapos-ai": "0.5.0",
"trapos": "0.6.1"
"trapos-ai": "0.5.1",
"trapos": "0.6.2"
}
}

View File

@ -1,6 +1,6 @@
{
"name": "trapos-ai",
"version": "0.5.0",
"version": "0.5.1",
"description": "TrapOS AI client for opencode serve",
"dependencies": ["trapos-core"],
"files": [

View File

@ -1,6 +1,6 @@
{
"name": "trapos",
"version": "0.6.1",
"version": "0.6.2",
"description": "TrapOS full install meta-package",
"dependencies": ["trapos-boot", "trapos-net", "trapos-ui", "trapos-test", "trapos-ai"],
"files": [],