92 lines
2.0 KiB
Lua
92 lines
2.0 KiB
Lua
local function defaultOs()
|
|
return os;
|
|
end
|
|
|
|
local function formatLabel(label)
|
|
if type(label) ~= 'string' or label == '' then
|
|
return 'null';
|
|
end
|
|
return label;
|
|
end
|
|
|
|
local function createMcpComputer()
|
|
local api = {};
|
|
|
|
api.formatLabel = formatLabel;
|
|
|
|
function api.formatPong(osLike)
|
|
osLike = osLike or defaultOs();
|
|
return 'pong from ' .. tostring(osLike.getComputerID())
|
|
.. ' (Label: ' .. formatLabel(osLike.getComputerLabel()) .. ')';
|
|
end
|
|
|
|
function api.parseArgs(args)
|
|
args = args or {};
|
|
local count = args.n or #args;
|
|
local url = nil;
|
|
|
|
if count == 0 then
|
|
return nil, 'missing websocket URL';
|
|
end
|
|
|
|
local i = 1;
|
|
while i <= count do
|
|
local arg = args[i];
|
|
if arg == '-url' then
|
|
if not args[i + 1] or args[i + 1] == '' then
|
|
return nil, 'missing value for -url';
|
|
end
|
|
url = args[i + 1];
|
|
i = i + 1;
|
|
elseif string.sub(tostring(arg), 1, 1) == '-' then
|
|
return nil, 'unknown option: ' .. tostring(arg);
|
|
elseif not url then
|
|
url = arg;
|
|
else
|
|
return nil, 'unexpected argument: ' .. tostring(arg);
|
|
end
|
|
i = i + 1;
|
|
end
|
|
|
|
if not url or url == '' then
|
|
return nil, 'missing websocket URL';
|
|
end
|
|
return { url = url };
|
|
end
|
|
|
|
function api.hello(osLike)
|
|
osLike = osLike or defaultOs();
|
|
return {
|
|
type = 'hello',
|
|
computerId = osLike.getComputerID(),
|
|
computerLabel = osLike.getComputerLabel(),
|
|
};
|
|
end
|
|
|
|
function api.handleRequest(request, osLike)
|
|
if type(request) ~= 'table' or request.type ~= 'request' or type(request.id) ~= 'string' then
|
|
return nil;
|
|
end
|
|
|
|
if request.method == 'ping' then
|
|
return {
|
|
type = 'response',
|
|
id = request.id,
|
|
ok = true,
|
|
result = api.formatPong(osLike),
|
|
};
|
|
end
|
|
|
|
return {
|
|
type = 'response',
|
|
id = request.id,
|
|
ok = false,
|
|
error = 'unknown method',
|
|
};
|
|
end
|
|
|
|
return api;
|
|
end
|
|
|
|
return createMcpComputer;
|