feat(ai): configure request timeout
This commit is contained in:
parent
c3813d6cda
commit
8467c30dd8
@ -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';
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "TrapOS",
|
||||
"version": "0.5.3",
|
||||
"version": "0.5.4",
|
||||
"branch": "next",
|
||||
"packages": [
|
||||
"trapos"
|
||||
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@ -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": [
|
||||
|
||||
@ -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": [],
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
local _VERSION = '0.3.0';
|
||||
local _VERSION = '0.4.0';
|
||||
|
||||
local createAi = require('/apis/libai');
|
||||
|
||||
|
||||
10
tests/ai.lua
10
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user