:LLeMbasLogin [address] [--ca file] runs the CLI's own sign-in (lembas login --json), shows the code, opens the approval page and restarts the agent when it is done; it says whose sign-in it replaces and asks first. :LLeMbasLoginCancel and :LLeMbasLogout. A server that asks for a newer plugin (min_client in the CLI's hello) is said once. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
197 lines
8.0 KiB
Lua
197 lines
8.0 KiB
Lua
-- Signing in and out from the editor: `lembas login --json <address>` and `lembas logout`, run with
|
|
-- the same program the plugin starts as its agent. The command line is a list, never a shell's; the
|
|
-- address is the person's own (nothing is predefined here); what the CLI prints is read as data.
|
|
local M = {}
|
|
|
|
local running = nil -- { proc, killer, cancelled }
|
|
|
|
local function L() return require("llembas") end
|
|
|
|
local function same_server(a, b)
|
|
local function bare(s) return (tostring(s or ""):gsub("^https?://", ""):gsub("/+$", "")):lower() end
|
|
return bare(a) ~= "" and bare(a) == bare(b)
|
|
end
|
|
|
|
--- The events `lembas login --json` wrote: one JSON object a line (`code`, `done`, `error`).
|
|
local function events_of(text)
|
|
local out = {}
|
|
for line in tostring(text or ""):gmatch("[^\r\n]+") do
|
|
local ok, ev = pcall(vim.json.decode, line, { luanil = { object = true, array = true } })
|
|
if ok and type(ev) == "table" and type(ev.event) == "string" then out[#out + 1] = ev end
|
|
end
|
|
return out
|
|
end
|
|
M._events_of = events_of
|
|
|
|
local function last_line(text)
|
|
local last = ""
|
|
for line in tostring(text or ""):gmatch("[^\r\n]+") do if line:match("%S") then last = line end end
|
|
return last
|
|
end
|
|
|
|
local function kill(proc)
|
|
pcall(function() proc:kill(15) end)
|
|
local t = vim.uv.new_timer()
|
|
t:start(require("llembas.rpc").KILL_AFTER_MS, 0, function()
|
|
pcall(function() t:stop(); t:close() end)
|
|
pcall(function() proc:kill(9) end)
|
|
end)
|
|
pcall(function() t:unref() end)
|
|
return t
|
|
end
|
|
|
|
--- The server this computer is signed in to now, or nil: `lembas login --json` with no address
|
|
--- reads the one login there is anew, and says whose it is. `cb(base_url | nil)`.
|
|
function M.current(exe, cb)
|
|
local done = false
|
|
local proc
|
|
local function finish(v)
|
|
if done then return end
|
|
done = true
|
|
vim.schedule(function() cb(v) end)
|
|
end
|
|
local ok
|
|
ok, proc = pcall(vim.system, { exe, "login", "--json" }, {
|
|
text = true,
|
|
stdout = function(_, data)
|
|
-- A sign-in the server no longer takes is begun anew by the CLI: that it asks for a code
|
|
-- says there was one. It is not gone through with here.
|
|
for _, ev in ipairs(events_of(data)) do
|
|
if ev.event == "code" and type(ev.instance) == "table" then
|
|
finish(ev.instance.base_url)
|
|
kill(proc)
|
|
elseif ev.event == "done" then finish(ev.base_url)
|
|
end
|
|
end
|
|
end,
|
|
timeout = 15000,
|
|
}, function() finish(nil) end)
|
|
if not ok then finish(nil) end
|
|
end
|
|
|
|
--- Stop a sign-in that is under way. False when none is.
|
|
function M.cancel()
|
|
local r = running
|
|
if not r then return false end
|
|
r.cancelled = true
|
|
kill(r.proc)
|
|
return true
|
|
end
|
|
|
|
function M.running() return running ~= nil end
|
|
|
|
local function run(exe, address, ca)
|
|
local words = L()._words
|
|
local argv = { exe, "login", "--json", address }
|
|
if ca and ca ~= "" then vim.list_extend(argv, { "--ca", vim.fn.expand(ca) }) end
|
|
local me = { cancelled = false }
|
|
local settled = false
|
|
local buf = ""
|
|
local function on_event(ev)
|
|
if ev.event == "code" then
|
|
local where = type(ev.instance) == "table" and (ev.instance.name or ev.instance.base_url) or address
|
|
local url = ev.verification_uri_complete or ev.verification_uri
|
|
L()._note(("To sign in to %s, approve the code %s at %s — the page is being opened in your browser. :LLeMbasLoginCancel stops waiting.")
|
|
:format(tostring(where), tostring(ev.user_code), tostring(ev.verification_uri)))
|
|
-- Only a web address is handed to the system, whatever the program printed.
|
|
if type(url) == "string" and url:match("^https?://[^%s]+$") and vim.ui.open then pcall(vim.ui.open, url) end
|
|
-- The CLI says when the code has run out; should it not, nothing waits for ever.
|
|
local limit = (tonumber(ev.expires_in) or 600) + 30
|
|
me.expiry = vim.defer_fn(function() if running == me then kill(me.proc) end end, limit * 1000)
|
|
elseif ev.event == "done" then
|
|
settled = true
|
|
if running == me then running = nil end
|
|
L().restart(true)
|
|
L()._note(("Signed in to %s%s. The next message starts a session."):format(tostring(ev.base_url or address),
|
|
tonumber(ev.models) and (" (%d models)"):format(ev.models) or ""))
|
|
elseif ev.event == "error" then
|
|
settled = true
|
|
if running == me then running = nil end
|
|
L()._note("Not signed in: " .. L()._say({ message = ev.message, code = ev.code }), "error")
|
|
end
|
|
end
|
|
local ok, proc = pcall(vim.system, argv, {
|
|
text = true,
|
|
stdout = function(_, data)
|
|
if not data then return end
|
|
buf = buf .. data
|
|
while true do
|
|
local nl = buf:find("\n", 1, true)
|
|
if not nl then break end
|
|
local line = buf:sub(1, nl - 1)
|
|
buf = buf:sub(nl + 1)
|
|
for _, ev in ipairs(events_of(line)) do vim.schedule(function() on_event(ev) end) end
|
|
end
|
|
end,
|
|
}, function(res)
|
|
vim.schedule(function()
|
|
if running == me then running = nil end
|
|
if me.expiry then pcall(function() me.expiry:stop(); me.expiry:close() end) end
|
|
if settled then return end
|
|
if me.cancelled then return L()._note("Signing in was cancelled.") end
|
|
local why = last_line(res.stderr)
|
|
L()._note("Not signed in: " .. (why ~= "" and why or ("the LLeMbas CLI ended (exit " .. tostring(res.code) .. ")")), "error")
|
|
end)
|
|
end)
|
|
if not ok then return L()._note(words.not_found:format(exe), "error") end
|
|
me.proc = proc
|
|
running = me
|
|
end
|
|
|
|
--- Sign this computer in to a LLeMbas server. `address`: asked for when not given. `ca`: a file
|
|
--- with the certificate authority of a server that has a private one.
|
|
function M.login(address, ca)
|
|
if running then return vim.notify("LLeMbas: a sign-in is under way (:LLeMbasLoginCancel stops it)", vim.log.levels.WARN) end
|
|
local cmd, why = L()._command()
|
|
if not cmd then return L()._note(why, "error") end
|
|
local exe = cmd[1]
|
|
if not address or not address:match("%S") then
|
|
return vim.ui.input({ prompt = "LLeMbas server address: " }, function(text)
|
|
if text and text:match("%S") then M.login(vim.trim(text), ca) end
|
|
end)
|
|
end
|
|
address = vim.trim(address)
|
|
ca = ca or (L().config.login or {}).ca
|
|
M.current(exe, function(now)
|
|
-- The same server again, without a certificate: the CLI reads its login anew and replaces nothing.
|
|
if not now or (same_server(now, address) and not (ca and ca ~= "")) then return run(exe, address, ca) end
|
|
local choices = {
|
|
{ go = false, label = "Cancel" },
|
|
{ go = true, label = ("Sign in to %s (replaces the sign-in to %s)"):format(address, now) },
|
|
}
|
|
vim.ui.select(choices, {
|
|
prompt = ("This computer is signed in to %s. The LLeMbas CLI keeps one sign-in: the terminal and the linked service use it too."):format(now),
|
|
format_item = function(c) return c.label end,
|
|
}, function(choice)
|
|
if choice and choice.go then run(exe, address, ca) end
|
|
end)
|
|
end)
|
|
end
|
|
|
|
--- Sign this computer out: the terminal and the linked service are signed out with it.
|
|
function M.logout()
|
|
local cmd, why = L()._command()
|
|
if not cmd then return L()._note(why, "error") end
|
|
local exe = cmd[1]
|
|
M.current(exe, function(now)
|
|
if not now then return vim.notify("LLeMbas: this computer is not signed in to a server", vim.log.levels.INFO) end
|
|
vim.ui.select({ { go = false, label = "Cancel" }, { go = true, label = "Sign out of " .. now } }, {
|
|
prompt = ("Sign this computer out of %s? The terminal and the linked service use the same sign-in."):format(now),
|
|
format_item = function(c) return c.label end,
|
|
}, function(choice)
|
|
if not choice or not choice.go then return end
|
|
vim.system({ exe, "logout" }, { text = true, timeout = 30000 }, function(res)
|
|
vim.schedule(function()
|
|
if res.code ~= 0 then
|
|
return L()._note("Not signed out: " .. (last_line(res.stderr) ~= "" and last_line(res.stderr) or ("exit " .. tostring(res.code))), "error")
|
|
end
|
|
L().restart(true)
|
|
L()._note(last_line(res.stdout) ~= "" and last_line(res.stdout) or ("Signed out of " .. now .. "."))
|
|
end)
|
|
end)
|
|
end)
|
|
end)
|
|
end
|
|
|
|
return M
|