fix(ai): cap request timeout
This commit is contained in:
parent
f6d6c9b5af
commit
ccd3dfef7b
@ -1,6 +1,7 @@
|
|||||||
local PING_PROMPT = 'reply with exactly: pong';
|
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_MAX_RETRIES = 2;
|
||||||
local DEFAULT_LUA_EXEC_TIMEOUT_SECONDS = 5;
|
local DEFAULT_LUA_EXEC_TIMEOUT_SECONDS = 5;
|
||||||
|
|
||||||
@ -152,8 +153,9 @@ local function createAi(opts)
|
|||||||
local raw = options.timeoutSeconds;
|
local raw = options.timeoutSeconds;
|
||||||
if raw == nil then raw = settingsLib.get('opencc.timeout_seconds'); end
|
if raw == nil then raw = settingsLib.get('opencc.timeout_seconds'); end
|
||||||
local n = tonumber(raw);
|
local n = tonumber(raw);
|
||||||
if n and n > 0 then return n; end
|
if not n or n <= 0 then n = DEFAULT_TIMEOUT_SECONDS; end
|
||||||
return DEFAULT_TIMEOUT_SECONDS;
|
if n > MAX_TIMEOUT_SECONDS then n = MAX_TIMEOUT_SECONDS; end
|
||||||
|
return n;
|
||||||
end
|
end
|
||||||
|
|
||||||
local function resolveLuaExecMaxRetries(options)
|
local function resolveLuaExecMaxRetries(options)
|
||||||
@ -195,35 +197,35 @@ local function createAi(opts)
|
|||||||
return headers;
|
return headers;
|
||||||
end
|
end
|
||||||
|
|
||||||
local function doGet(cfg, path)
|
local function callHttp(method, request)
|
||||||
local response, _, errorResponse = httpLib.get({
|
local ok, response, httpErr, errorResponse = pcall(httpLib[method], request);
|
||||||
url = cfg.url .. path,
|
if not ok then
|
||||||
headers = buildHeaders(cfg),
|
return nil, 'http ' .. method .. ' threw: ' .. tostring(response);
|
||||||
timeout = cfg.timeoutSeconds,
|
end
|
||||||
});
|
|
||||||
response = response or errorResponse;
|
response = response or errorResponse;
|
||||||
if not response then
|
if not response then
|
||||||
return nil, 'serveur injoignable';
|
return nil, 'serveur injoignable: ' .. tostring(httpErr or 'unknown error');
|
||||||
end
|
end
|
||||||
local code = statusCode(response);
|
local code = statusCode(response);
|
||||||
local body = readAllAndClose(response);
|
local body = readAllAndClose(response);
|
||||||
return body, code;
|
return body, code;
|
||||||
end
|
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 function doPost(cfg, path, payload)
|
||||||
local response, _, errorResponse = httpLib.post({
|
return callHttp('post', {
|
||||||
url = cfg.url .. path,
|
url = cfg.url .. path,
|
||||||
body = textutils.serializeJSON(payload),
|
body = textutils.serializeJSON(payload),
|
||||||
headers = buildHeaders(cfg),
|
headers = buildHeaders(cfg),
|
||||||
timeout = cfg.timeoutSeconds,
|
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
|
end
|
||||||
|
|
||||||
function api.clearSession()
|
function api.clearSession()
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "TrapOS",
|
"name": "TrapOS",
|
||||||
"version": "0.6.1",
|
"version": "0.6.2",
|
||||||
"branch": "next",
|
"branch": "next",
|
||||||
"packages": [
|
"packages": [
|
||||||
"trapos"
|
"trapos"
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
"trapos-boot": "0.2.2",
|
"trapos-boot": "0.2.2",
|
||||||
"trapos-net": "0.2.1",
|
"trapos-net": "0.2.1",
|
||||||
"trapos-ui": "0.2.2",
|
"trapos-ui": "0.2.2",
|
||||||
"trapos-ai": "0.5.0",
|
"trapos-ai": "0.5.1",
|
||||||
"trapos": "0.6.1"
|
"trapos": "0.6.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "trapos-ai",
|
"name": "trapos-ai",
|
||||||
"version": "0.5.0",
|
"version": "0.5.1",
|
||||||
"description": "TrapOS AI client for opencode serve",
|
"description": "TrapOS AI client for opencode serve",
|
||||||
"dependencies": ["trapos-core"],
|
"dependencies": ["trapos-core"],
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "trapos",
|
"name": "trapos",
|
||||||
"version": "0.6.1",
|
"version": "0.6.2",
|
||||||
"description": "TrapOS full install meta-package",
|
"description": "TrapOS full install meta-package",
|
||||||
"dependencies": ["trapos-boot", "trapos-net", "trapos-ui", "trapos-test", "trapos-ai"],
|
"dependencies": ["trapos-boot", "trapos-net", "trapos-ui", "trapos-test", "trapos-ai"],
|
||||||
"files": [],
|
"files": [],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user