feat(ai): configure request timeout

This commit is contained in:
Guillaume ARM 2026-06-09 05:35:46 +02:00
parent c3813d6cda
commit 8467c30dd8
7 changed files with 40 additions and 18 deletions

View File

@ -1,7 +1,9 @@
local _VERSION = '0.3.0'; local _VERSION = '0.4.0';
local PING_PROMPT = 'reply with exactly: pong'; local PING_PROMPT = 'reply with exactly: pong';
local DEFAULT_TIMEOUT_SECONDS = 1200;
local B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; local B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
local function base64encode(s) local function base64encode(s)
@ -66,6 +68,14 @@ local function createAi(opts)
return _VERSION; return _VERSION;
end end
local function resolveTimeout(options)
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;
end
local function resolveConfig(options) local function resolveConfig(options)
local url = options.serverUrl or settingsLib.get('opencc.server_url'); local url = options.serverUrl or settingsLib.get('opencc.server_url');
if not url or url == '' then if not url or url == '' then
@ -73,7 +83,12 @@ local function createAi(opts)
end end
local username = options.username or settingsLib.get('opencc.username') or 'opencode'; local username = options.username or settingsLib.get('opencc.username') or 'opencode';
local password = options.password or settingsLib.get('opencc.password') or ''; local password = options.password or settingsLib.get('opencc.password') or '';
return { url = trimTrailingSlash(url), username = username, password = password }; return {
url = trimTrailingSlash(url),
username = username,
password = password,
timeoutSeconds = resolveTimeout(options),
};
end end
local function buildHeaders(cfg) local function buildHeaders(cfg)
@ -88,7 +103,11 @@ local function createAi(opts)
end end
local function doGet(cfg, path) local function doGet(cfg, path)
local response, _, errorResponse = httpLib.get(cfg.url .. path, buildHeaders(cfg)); local response, _, errorResponse = httpLib.get({
url = cfg.url .. path,
headers = buildHeaders(cfg),
timeout = cfg.timeoutSeconds,
});
response = response or errorResponse; response = response or errorResponse;
if not response then if not response then
return nil, 'serveur injoignable'; return nil, 'serveur injoignable';
@ -99,11 +118,12 @@ local function createAi(opts)
end end
local function doPost(cfg, path, payload) local function doPost(cfg, path, payload)
local response, _, errorResponse = httpLib.post( local response, _, errorResponse = httpLib.post({
cfg.url .. path, url = cfg.url .. path,
textutils.serializeJSON(payload), body = textutils.serializeJSON(payload),
buildHeaders(cfg) headers = buildHeaders(cfg),
); timeout = cfg.timeoutSeconds,
});
response = response or errorResponse; response = response or errorResponse;
if not response then if not response then
return nil, 'serveur injoignable'; return nil, 'serveur injoignable';

View File

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

View File

@ -5,7 +5,7 @@
"trapos-boot": "0.2.1", "trapos-boot": "0.2.1",
"trapos-net": "0.2.0", "trapos-net": "0.2.0",
"trapos-ui": "0.2.1", "trapos-ui": "0.2.1",
"trapos-ai": "0.3.0", "trapos-ai": "0.4.0",
"trapos": "0.5.3" "trapos": "0.5.4"
} }
} }

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name": "trapos", "name": "trapos",
"version": "0.5.3", "version": "0.5.4",
"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": [],

View File

@ -1,4 +1,4 @@
local _VERSION = '0.3.0'; local _VERSION = '0.4.0';
local createAi = require('/apis/libai'); local createAi = require('/apis/libai');

View File

@ -34,15 +34,17 @@ local function fakeHttp(postResults, getResults)
local postIdx = 0; local postIdx = 0;
local getIdx = 0; local getIdx = 0;
return { return {
post = function(url, body, headers) post = function(req)
postCalls[#postCalls + 1] = { url = url, body = body, headers = headers }; local url, body, headers, timeout = req.url, req.body, req.headers, req.timeout;
postCalls[#postCalls + 1] = { url = url, body = body, headers = headers, timeout = timeout };
postIdx = postIdx + 1; postIdx = postIdx + 1;
local r = postResults[postIdx]; local r = postResults[postIdx];
if type(r) == 'function' then return r(url, body, headers); end if type(r) == 'function' then return r(url, body, headers); end
return r; return r;
end, end,
get = function(url, headers) get = function(req)
getCalls[#getCalls + 1] = { url = url, headers = headers }; local url, headers, timeout = req.url, req.headers, req.timeout;
getCalls[#getCalls + 1] = { url = url, headers = headers, timeout = timeout };
getIdx = getIdx + 1; getIdx = getIdx + 1;
local r = getResults[getIdx]; local r = getResults[getIdx];
if type(r) == 'function' then return r(url, headers); end if type(r) == 'function' then return r(url, headers); end