From ccd3dfef7b3017b18393e442f76e788c3abc3f71 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Tue, 9 Jun 2026 08:33:24 +0200 Subject: [PATCH] fix(ai): cap request timeout --- apis/libai.lua | 38 +++++++++++++++++++----------------- manifest.json | 2 +- packages/index.json | 4 ++-- packages/trapos-ai/ccpm.json | 2 +- packages/trapos/ccpm.json | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/apis/libai.lua b/apis/libai.lua index 97681a8..4285bb0 100644 --- a/apis/libai.lua +++ b/apis/libai.lua @@ -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() diff --git a/manifest.json b/manifest.json index c5951fc..5ccda29 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "TrapOS", - "version": "0.6.1", + "version": "0.6.2", "branch": "next", "packages": [ "trapos" diff --git a/packages/index.json b/packages/index.json index e2d1056..709e2c9 100644 --- a/packages/index.json +++ b/packages/index.json @@ -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" } } diff --git a/packages/trapos-ai/ccpm.json b/packages/trapos-ai/ccpm.json index 45ade31..a629bf9 100644 --- a/packages/trapos-ai/ccpm.json +++ b/packages/trapos-ai/ccpm.json @@ -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": [ diff --git a/packages/trapos/ccpm.json b/packages/trapos/ccpm.json index 9511aa3..c4fd092 100644 --- a/packages/trapos/ccpm.json +++ b/packages/trapos/ccpm.json @@ -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": [],