-- LLeMbas, built in. The plugin is the one in this repository's nvim/ directory, used through -- its documented commands and setup; the agent is the LLeMbas CLI. Nothing about a server, a -- model or a key is set here: they come from the CLI's sign-in. local M = {} M.loaded = false --- The agent's program, or nil: `lembas` on PATH or where its installer puts it, or whatever --- `cmd` was set to. The plugin decides (`require("llembas").cli()`): there is one rule for --- "is the LLeMbas CLI installed", and the launcher's `doctor` keeps the same one in shell. function M.cli() local ok, llembas = pcall(require, "llembas") if not ok or type(llembas.cli) ~= "function" then return nil end local cmd = llembas.cli() return cmd and cmd[1] or nil end function M.has_cli() return M.cli() ~= nil end local signed = nil -- true | false once it was looked up; nil before, and when it cannot be told --- Whether the CLI is signed in to a server: true, false, or nil when that is not known (yet). --- Looked up once, on this machine only — where the CLI keeps its sign-ins (`lembas __paths`), --- as `llembas-ide doctor` does; no server is asked. `cb()` is called when the answer is in. function M.signed_in(cb) if signed ~= nil or M.looking then return signed end local exe = M.cli() if not exe then return nil end M.looking = true local ok = pcall(vim.system, { exe, "__paths" }, { text = true, timeout = 5000 }, function(res) local dir = res.code == 0 and (res.stdout or ""):match("config=([^\r\n]+)") or nil vim.schedule(function() M.looking = false if dir then local f = io.open(dir .. "/lembas.json", "r") local text = f and f:read("*a") or "" if f then f:close() end signed = text:find('"base_url"', 1, true) ~= nil end if cb then cb() end end) end) if not ok then M.looking = false end return nil end --- The layout LLeMbas IDE starts with: all four bars. `lua/user/init.lua` changes it with --- require("llembas").setup({ bars = { … } }); see |llembas-bars|. M.bars = { top = true, bottom = true, tree = true, chat = true, tree_width = 26, chat_width = 42 } function M.setup() local ok, llembas = pcall(require, "llembas") if not ok then table.insert(require("llembas_ide").problems, "llembas: the LLeMbas plugin is not installed") return end llembas.setup({ -- Without the CLI the IDE is an editor and stays quiet: no suggestions are asked for. complete = { auto = M.has_cli() }, -- The keys are the plugin's own table, mapped in both forms; an action that needs the agent -- says what is missing instead of failing when there is no CLI. keys = { prefix = "l", guard = function() if M.has_cli() then return true end M.sign_in() return false end, }, bars = vim.deepcopy(M.bars), }) M.loaded = true end --- The plugin's version, or nil. function M.version() local ok, llembas = pcall(require, "llembas") return ok and llembas.version or nil end --- Whether suggestions as you type are on. function M.autocomplete_on() local ok, llembas = pcall(require, "llembas") return ok and type(llembas.config) == "table" and type(llembas.config.complete) == "table" and llembas.config.complete.auto == true end --- For the statusline: what LLeMbas is doing, in a few characters. function M.status() if not M.loaded then return "", false end if not M.has_cli() then return "◈ no CLI", false end local ok, text = pcall(function() return require("llembas").statusline() end) text = (ok and type(text) == "string" and text ~= "") and text or "◈ LLeMbas" return text .. (M.autocomplete_on() and " · suggest on" or " · suggest off"), true end --- Signing in is the CLI's. With a plugin that offers :LLeMbasLogin that is used; otherwise the --- one command to type is shown. function M.sign_in() if vim.fn.exists(":LLeMbasLogin") == 2 then return vim.cmd("LLeMbasLogin") end local lines if M.has_cli() then lines = { "LLeMbas needs two things: a LLeMbas server, and a sign-in to it.", "", "In a terminal (tt opens one here), with your server's address:", "", " lembas login https://your-server.example.org", "", "It shows a code to confirm in the browser. After that the chat (ll),", "asking about code and suggestions as you type work in this editor.", "The models and the permissions are your account's; nothing is set here.", } else lines = { "The LLeMbas CLI (the `lembas` command) is not installed.", "", "It is the agent this editor talks to. Everything else in the IDE works without it.", "To get it, run the installer again and answer yes to the CLI, or see", "", " https://github.com/LLeMbas/LLeMbas-CLI", "", "Then sign in to your LLeMbas server: lembas login https://your-server.example.org", } end require("llembas_ide.ui").page("Sign in to LLeMbas", lines) end --- Run a plugin command only when the plugin and the CLI are there; otherwise say what is missing. function M.cmd(command) return function() if not M.loaded then return vim.notify("LLeMbas: the plugin is not installed", vim.log.levels.WARN) end if not M.has_cli() then return M.sign_in() end vim.cmd(command) end end return M