115 lines
3.0 KiB
Lua
115 lines
3.0 KiB
Lua
local _VERSION = '0.1.0';
|
|
|
|
local DEFAULT_PROMPT = 'reply with exactly: pong';
|
|
|
|
local function trimTrailingSlash(value)
|
|
return (value:gsub('/+$', ''));
|
|
end
|
|
|
|
local function readAllAndClose(response)
|
|
local body = response.readAll();
|
|
response.close();
|
|
return body;
|
|
end
|
|
|
|
local function statusCode(response)
|
|
if response.getResponseCode then
|
|
return response.getResponseCode();
|
|
end
|
|
return nil;
|
|
end
|
|
|
|
local function mapStatusError(code)
|
|
if code == 401 then return 'token invalide'; end
|
|
if code == 413 then return 'prompt trop long'; end
|
|
if code == 502 then return 'agent indisponible'; end
|
|
if code == 504 then return 'agent trop lent'; end
|
|
if code then return 'erreur proxy: HTTP ' .. tostring(code); end
|
|
return 'erreur proxy inconnue';
|
|
end
|
|
|
|
local function createAiHelloWorld(opts)
|
|
opts = opts or {};
|
|
|
|
local httpLib = opts.http or http;
|
|
local settingsLib = opts.settings or settings;
|
|
local prompt = opts.prompt or DEFAULT_PROMPT;
|
|
|
|
local api = {};
|
|
|
|
function api.version()
|
|
return _VERSION;
|
|
end
|
|
|
|
function api.clearSession()
|
|
settingsLib.unset('opencc.session_id');
|
|
if settingsLib.save then settingsLib.save(); end
|
|
end
|
|
|
|
function api.askHello(options)
|
|
options = options or {};
|
|
|
|
local proxyUrl = options.proxyUrl or settingsLib.get('opencc.proxy_url');
|
|
if not proxyUrl or proxyUrl == '' then
|
|
return false, 'missing opencc.proxy_url; run: settings set opencc.proxy_url <url>';
|
|
end
|
|
|
|
local token = options.proxyToken or settingsLib.get('opencc.proxy_token');
|
|
if not token or token == '' then
|
|
return false, 'missing opencc.proxy_token; run: settings set opencc.proxy_token <token>';
|
|
end
|
|
|
|
local sessionId = options.sessionId;
|
|
if sessionId == nil then
|
|
sessionId = settingsLib.get('opencc.session_id');
|
|
end
|
|
|
|
local body = { prompt = prompt };
|
|
if sessionId and sessionId ~= '' then
|
|
body.sessionId = sessionId;
|
|
end
|
|
|
|
local response = httpLib.post(
|
|
trimTrailingSlash(proxyUrl) .. '/ask',
|
|
textutils.serializeJSON(body),
|
|
{
|
|
['Authorization'] = 'Bearer ' .. token,
|
|
['Content-Type'] = 'application/json',
|
|
}
|
|
);
|
|
|
|
if not response then
|
|
return false, 'proxy injoignable. Verifie opencc.proxy_url et http.rules';
|
|
end
|
|
|
|
local code = statusCode(response);
|
|
local responseBody = readAllAndClose(response);
|
|
if code and code ~= 200 then
|
|
return false, mapStatusError(code);
|
|
end
|
|
|
|
local decoded = textutils.unserializeJSON(responseBody or '');
|
|
if type(decoded) ~= 'table' then
|
|
return false, 'reponse proxy invalide';
|
|
end
|
|
if type(decoded.reply) ~= 'string' then
|
|
return false, 'reponse proxy sans reply';
|
|
end
|
|
|
|
if type(decoded.sessionId) == 'string' and decoded.sessionId ~= '' then
|
|
settingsLib.set('opencc.session_id', decoded.sessionId);
|
|
if settingsLib.save then settingsLib.save(); end
|
|
end
|
|
|
|
return true, {
|
|
reply = decoded.reply,
|
|
sessionId = decoded.sessionId,
|
|
truncated = decoded.truncated == true,
|
|
};
|
|
end
|
|
|
|
return api;
|
|
end
|
|
|
|
return createAiHelloWorld;
|