From 8467c30dd8773be0c5a538695d859b15e2af7c5d Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Tue, 9 Jun 2026 05:35:46 +0200 Subject: [PATCH] feat(ai): configure request timeout --- apis/libai.lua | 36 ++++++++++++++++++++++++++++-------- manifest.json | 2 +- packages/index.json | 4 ++-- packages/trapos-ai/ccpm.json | 2 +- packages/trapos/ccpm.json | 2 +- programs/ai.lua | 2 +- tests/ai.lua | 10 ++++++---- 7 files changed, 40 insertions(+), 18 deletions(-) diff --git a/apis/libai.lua b/apis/libai.lua index 986dc35..e9337b5 100644 --- a/apis/libai.lua +++ b/apis/libai.lua @@ -1,7 +1,9 @@ -local _VERSION = '0.3.0'; +local _VERSION = '0.4.0'; local PING_PROMPT = 'reply with exactly: pong'; +local DEFAULT_TIMEOUT_SECONDS = 1200; + local B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; local function base64encode(s) @@ -66,6 +68,14 @@ local function createAi(opts) return _VERSION; 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 url = options.serverUrl or settingsLib.get('opencc.server_url'); if not url or url == '' then @@ -73,7 +83,12 @@ local function createAi(opts) end local username = options.username or settingsLib.get('opencc.username') or 'opencode'; 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 local function buildHeaders(cfg) @@ -88,7 +103,11 @@ local function createAi(opts) end 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; if not response then return nil, 'serveur injoignable'; @@ -99,11 +118,12 @@ local function createAi(opts) end local function doPost(cfg, path, payload) - local response, _, errorResponse = httpLib.post( - cfg.url .. path, - textutils.serializeJSON(payload), - buildHeaders(cfg) - ); + local response, _, errorResponse = httpLib.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'; diff --git a/manifest.json b/manifest.json index 2046f4e..f2520f4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "TrapOS", - "version": "0.5.3", + "version": "0.5.4", "branch": "next", "packages": [ "trapos" diff --git a/packages/index.json b/packages/index.json index d412981..51163c2 100644 --- a/packages/index.json +++ b/packages/index.json @@ -5,7 +5,7 @@ "trapos-boot": "0.2.1", "trapos-net": "0.2.0", "trapos-ui": "0.2.1", - "trapos-ai": "0.3.0", - "trapos": "0.5.3" + "trapos-ai": "0.4.0", + "trapos": "0.5.4" } } diff --git a/packages/trapos-ai/ccpm.json b/packages/trapos-ai/ccpm.json index c9c4580..9ac6dfe 100644 --- a/packages/trapos-ai/ccpm.json +++ b/packages/trapos-ai/ccpm.json @@ -1,6 +1,6 @@ { "name": "trapos-ai", - "version": "0.3.0", + "version": "0.4.0", "description": "TrapOS AI client for opencode serve", "dependencies": ["trapos-core"], "files": [ diff --git a/packages/trapos/ccpm.json b/packages/trapos/ccpm.json index 4505b1f..76d125a 100644 --- a/packages/trapos/ccpm.json +++ b/packages/trapos/ccpm.json @@ -1,6 +1,6 @@ { "name": "trapos", - "version": "0.5.3", + "version": "0.5.4", "description": "TrapOS full install meta-package", "dependencies": ["trapos-boot", "trapos-net", "trapos-ui", "trapos-test", "trapos-ai"], "files": [], diff --git a/programs/ai.lua b/programs/ai.lua index 33a5fbd..f64d9ec 100644 --- a/programs/ai.lua +++ b/programs/ai.lua @@ -1,4 +1,4 @@ -local _VERSION = '0.3.0'; +local _VERSION = '0.4.0'; local createAi = require('/apis/libai'); diff --git a/tests/ai.lua b/tests/ai.lua index e114aa0..1fd8be8 100644 --- a/tests/ai.lua +++ b/tests/ai.lua @@ -34,15 +34,17 @@ local function fakeHttp(postResults, getResults) local postIdx = 0; local getIdx = 0; return { - post = function(url, body, headers) - postCalls[#postCalls + 1] = { url = url, body = body, headers = headers }; + post = function(req) + 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; local r = postResults[postIdx]; if type(r) == 'function' then return r(url, body, headers); end return r; end, - get = function(url, headers) - getCalls[#getCalls + 1] = { url = url, headers = headers }; + get = function(req) + local url, headers, timeout = req.url, req.headers, req.timeout; + getCalls[#getCalls + 1] = { url = url, headers = headers, timeout = timeout }; getIdx = getIdx + 1; local r = getResults[getIdx]; if type(r) == 'function' then return r(url, headers); end