Files
LLeMbas-CLI/src/tui/commands.ts
T
2026-12-01 20:36:18 +00:00

1604 lines
79 KiB
TypeScript

// Built-in slash commands; custom markdown commands, skills and MCP prompts join them in the
// palette.
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
import { terminalCommands } from "../terminal/command.ts"
import { join } from "node:path"
import { paths } from "../config/paths.ts"
import { asMode, MODES, type Mode } from "../config/schema.ts"
import { levelsFor, type Level } from "../provider/effort.ts"
import { setTrust } from "../project/root.ts"
import { commit, commitPlan, draftMessage } from "../git/commit.ts"
import { doRelease, planRelease } from "../git/release.ts"
import { instructionFiles } from "../prompt/assemble.ts"
import { addDecision, readBoard, readDecisions, renderBoard } from "../project/board.ts"
import { contextBreakdown, renderBreakdown } from "../session/context.ts"
import { SUBAGENTS } from "../team/subagents.ts"
import { TEAM } from "../team/team.ts"
import { changelogPrompt, initPrompt, reviewPrompt, teamText } from "../session/commands.ts"
import { git } from "../git/run.ts"
import { branches, deleteBranch, switchBranch } from "../git/branch.ts"
import type { Ctx } from "./context.ts"
import type { DialogSpec, Option } from "./components/dialog.tsx"
import { deleteEverywhere } from "../acp/share.ts"
import { themeNames } from "./theme.ts"
import { render } from "../plan/plan.ts"
import { planTitle } from "../tool/plan_write.ts"
import { findSetting, show, type Scope } from "../config/settings.ts"
import { ingest } from "../library/ingest.ts"
import { findSkill, skillMessage, slug } from "../skill/index.ts"
import { PERSONALITIES, personalityText } from "../prompt/personality.ts"
import { listPages } from "../pages/store.ts"
import { keyMatcher } from "../voice/index.ts"
import { PLAYER_NAMES, playerFor, recorderFor } from "../voice/audio.ts"
import { duration } from "../duration.ts"
import { LOOP } from "../harness.ts"
import { openMcpMenu, openMcpServer, runAction } from "./mcpmenu.ts"
import { openAgents, openHelp, openPermissions, openStatus } from "./menus.ts"
import { openReports, readReport } from "./reportsmenu.ts"
import type { Job, JobInfo } from "../jobs/registry.ts"
export interface CommandCtx extends Ctx {
voice: import("../voice/index.ts").VoiceController
/** Put text into the prompt (e.g. the prompt /undo took back). */
setInput(text: string): void
/** Copy to the clipboard (OSC 52 and, on this machine, the system clipboard), and say how it went. */
copy(text: string): void
renderer: import("@opentui/core").CliRenderer
/** Send a prompt as if typed. */
send?(text: string): void
/** The session shared with the web UI. */
share?: import("../acp/share.ts").Share
/** Carry on: after a stop (esc), or an answer that ended too soon (ctrl+g). */
continueTask(): void
/** Send a prompt shown as `shown`, with a model/mode for this prompt only. */
sendAs?(prompt: string, shown: string, opts?: { model?: string; mode?: string; attachFrom?: string }): void
openDialog(spec: DialogSpec): void
/** Run a line as if it had been typed and sent — a command chosen in a menu goes through the
* same checks as one typed (not mid-reply, and so on). false: refused. */
runLine(line: string): boolean | void
/** Run a custom command (a file under commands/) by its name; false when there is none. */
runCustom(name: string, arg: string, typed: string): boolean
setMode(m: Mode): void
setEffort(e: Level): void
setTheme(name: string): void
setIcons(set: import("./theme.ts").IconSet): void
/** A short word in the status bar that goes by itself. */
flash(text: string, ms?: number): void
refresh(): void
quit(): void
/** Start again: the binary on disk now, the config read afresh, this session. */
reload(): void
/** The status bar's month's cost, as /usage just read it. */
setSpend?(text: string): void
/** Open a command job's live output (components/jobview.tsx). */
openJob(id: string): void
/** Enter a subagent job: its transcript in place of the session's, the prompt box sends to it. */
enterJob(id: string): void
/** Control the side display pane (on/off/toggle or select a tab). */
setDisplays?(action: "toggle" | "on" | "off" | "live" | "files" | "changes" | "bash" | "images"): string
/** Which transcript is drawn: full, compact, auto (compact beside the display) — or the other
* of the two that is showing. Returns what to say. */
setTranscript?(to: "auto" | "full" | "compact" | "toggle"): string
}
export interface Command {
name: string
aliases?: string[]
args?: string
description: string
run(c: CommandCtx, arg: string): void | Promise<void>
}
const HELP_KEYS = [
"enter send · shift+enter / ctrl+j new line · ↑↓ history",
"esc stop the model · ctrl+g continue (after a stop, or to keep going) · ctrl+c clear, twice to quit · ctrl+d (empty box) quit",
"/jobs background jobs: enter opens one (a subagent: esc back, type to message it), k stops it · ctrl+b moves the running command to the background",
"shift+tab cycle mode (manual → edit → plan) · ctrl+r show reasoning · ctrl+o expand tool output",
"alt+d the display (beside the conversation; on a narrow terminal in its place) · alt+↑/↓ its tab (live/files/changes/bash/images) · alt+←/→ entry · alt+pgup/pgdn scroll it · click a tab, wheel over it",
"alt+t transcript full ↔ compact (compact: a round of tool calls is one line, the detail in the display)",
"pgup/pgdn scroll half a screen · ctrl+↑/↓ three lines · the mouse wheel · ↑ in an empty box takes back a message waiting to go in",
]
const AGENT_TEMPLATE = (name: string) => `---
description: what ${name} is for — the model reads this when it picks a subagent
# mode: plan # plan: read-only; edit; manual (asks you) — never looser than yours
# tools: [read, glob, grep, list, bash, web_search, web_fetch] # default: every tool a subagent may have
# isolation: worktree # work in a git checkout and branch of its own
---
You are the ${name} subagent. Say what to do, how to check it, and what to report back — the
report is all the session's model sees of your work.
`
const TEAM_ROW = "\u0000team"
/** The Subagents setting as one range from 1 to the cap with two handles, Min and Max (harness
* team.md §2): the numbers count this agent, so 1 is this agent alone. */
function teamRange(c: CommandCtx): Option {
const t = c.app.team.team
const S = SUBAGENTS.setting
return {
label: S.label,
value: TEAM_ROW,
// The session's model cannot serve subagents itself: said where the size is set (subagents.md §4).
description: t.couldServe().own ? S.short : S.own,
range: { from: 1, to: t.cap, lo: t.min, hi: t.max, names: [S.min, S.max], onSubmit: (lo, hi) => applyTeamSize(c, `${lo} ${hi}`) },
}
}
const applyTeamSize = (c: CommandCtx, v: string) => (c.view.info(teamText(c.app, `size ${v}`)), c.refresh())
/** The team's size for this session: the range, a few usual pairs, or any typed as `<min> <max>`. */
function openTeamSize(c: CommandCtx) {
const t = c.app.team.team
const pairs = [
[1, 1, "this agent alone: no subagents"],
[1, 4, "this agent decides; up to three subagents"],
[2, 4, "subagents from the start"],
[1, 8, "this agent decides; up to seven subagents"],
[3, 8, "at least two subagents"],
[1, Math.min(16, t.cap), "many subagents, with sub-managers"],
] as const
const S = SUBAGENTS.setting
const seen = new Set<string>()
const options = pairs
.filter(([min, max]) => max <= t.cap && !seen.has(`${min} ${max}`) && seen.add(`${min} ${max}`))
.map(([min, max, what]) => ({ label: `${S.min} ${min}, ${S.max} ${max}`, value: `${min} ${max}`, description: what, current: t.min === min && t.max === max }))
c.openDialog({
title: `${S.label} — ${S.min} ${t.min}, ${S.max} ${t.max} (1 to ${t.cap}, this agent included)`,
options: [teamRange(c), ...options],
onSelect: (v) => applyTeamSize(c, v),
custom: { label: "set to <min> <max>", onSubmit: (v) => applyTeamSize(c, v) },
})
}
export const COMMANDS: Command[] = [
{
name: "agents",
args: "[new <name> [project] | <name> <task>]",
description: "subagents: list them, make one, or hand one a task",
run(c, arg) {
const [first, ...rest] = arg.trim().split(/\s+/).filter(Boolean)
const list = c.app.agents
if (first === "new") {
const name = (rest[0] ?? "").toLowerCase().replace(/[^a-z0-9_-]/g, "-")
if (!name) return c.view.info("/agents new <name> [project] — writes a starter file to edit", "warn")
const inProject = rest[1] === "project"
if (inProject && !c.app.trusted) return c.view.info("a project's own agents need a trusted project (/trust)", "warn")
const dir = inProject ? join(c.app.project.dir, "agents") : join(paths.config, "agents")
const file = join(dir, `${name}.md`)
if (existsSync(file)) return c.view.info(`${file} already exists`, "warn")
mkdirSync(dir, { recursive: true })
writeFileSync(file, AGENT_TEMPLATE(name))
return c.view.info(`wrote ${file} — fill it in, then /reload so the model is offered it`)
}
const named = first ? list.find((a) => a.name === first) : undefined
if (first && !named) return c.view.info(`no agent "${first}" — ${list.map((a) => a.name).join(", ")}`, "warn")
if (named && rest.length) {
if (c.app.turns.current) return c.view.info("wait for the task to finish (esc stops it)", "warn")
// Handed to the session's model, which runs it through the task tool and gets the report.
const task = rest.join(" ")
return c.send?.(`Use the ${named.name} subagent (the task tool, agent: "${named.name}") for this, then tell me what it reported:\n\n${task}`)
}
const describe = (a: (typeof list)[number]) =>
[
`${a.name} (${a.source}) — ${a.description}`,
` model the session's, or one you name${a.model ? ` (its own model: ${a.model} is not used)` : ""} · mode ${a.mode ?? "the session's"} · tools ${a.tools?.join(", ") ?? "all a subagent may have"}${a.isolation ? " · own worktree" : ""}`,
].join("\n")
if (named) return c.view.info(describe(named))
if (!first) return openAgents(c)
c.view.info(
[
...list.map(describe),
"",
"The model hands work to one through its task tool when that helps; you can ask it to (\"use the explore subagent to…\"), or /agents <name> <task>.",
`Your own: /agents new <name> writes ${join(paths.config, "agents")}/<name>.md (add \`project\` for this project's .agent/agents/).`,
].join("\n"),
)
},
},
{
name: "continue",
description: "carry on — after esc stopped it, or when it should keep going (ctrl+g)",
run: (c) => c.continueTask(),
},
{
name: "display",
aliases: ["displays"],
args: "[on|off|live|files|changes|bash|images]",
description: "the display pane: show or hide it, or open one of its tabs (live activity, files, changes, bash, images)",
run(c, arg) {
if (!c.setDisplays) return c.view.info("the display is not available here", "warn")
const a = arg.trim().toLowerCase()
if (!a) return c.view.info(c.setDisplays("toggle"))
if (a === "on" || a === "off" || a === "live" || a === "files" || a === "changes" || a === "bash" || a === "images") {
return c.view.info(c.setDisplays(a))
}
c.view.info("usage: /display [on|off|live|files|changes|bash|images] — pictures are drawn as the `images` setting says (auto, on, off: /settings)", "warn")
},
},
{
name: "transcript",
args: "[auto|full|compact]",
description: "the conversation in full, or compact (a round of tool calls as one line) — auto: compact beside the display",
run(c, arg) {
if (!c.setTranscript) return c.view.info("the transcript cannot be switched here", "warn")
const a = arg.trim().toLowerCase()
if (!a) return c.view.info(c.setTranscript("toggle"))
if (a === "auto" || a === "full" || a === "compact") return c.view.info(c.setTranscript(a))
c.view.info("usage: /transcript [auto|full|compact]", "warn")
},
},
{
name: "help",
description: "commands and keys",
run(c) {
// Through the same door as a typed line: what may not start mid-reply is refused there.
openHelp(c, COMMANDS, HELP_KEYS, (cmd) => void c.runLine(`/${cmd.name}`))
},
},
{
name: "permissions",
description: "the permission rules in force — this session's, the project's, yours, the built-in ones; change or add one",
run: (c) => openPermissions(c),
},
{
name: "status",
description: "everything in one place: the server and login, the session, the project, the MCP servers, this terminal",
run(c) {
openStatus(c, (line) => void c.runLine(`/${line}`))
},
},
{
name: "model",
args: "[model]",
description: "switch model",
run(c, arg) {
const refs = c.app.modelRefs()
const pick = (ref: string) => {
try {
c.app.settings.set("model", ref)
c.view.info(`model: ${ref}`)
} catch (e) {
c.view.info((e as Error).message, "warn")
}
}
if (arg) return pick(arg)
c.openDialog({
title: "Model",
options: refs.map((r) => {
const known = c.app.loaded.refs.find((x) => x.ref === r)!
const spec = c.app.loaded.connections[known.connection]!.models[known.id]!
const bits = [spec.context ? `${Math.round(spec.context / 1024)}k ctx` : "", spec.vision ? "vision" : "", levelsFor(c.app.engine.model.ref === r ? c.app.engine.model : { ...c.app.engine.model, ref: r, spec }).control === "switch" ? "thinking switch" : spec.efforts?.length ? "effort" : "", spec.interactive_only ? "interactive use only" : ""].filter(Boolean).join(" · ")
return { label: r, value: r, description: bits, current: r === c.app.engine.model.ref }
}),
onSelect: pick,
})
},
},
{
name: "effort",
args: "[level|on|off]",
description: "how hard the model thinks: its levels, a Thinking switch, or nothing to choose",
run(c, arg) {
// The control follows the model's levels (harness models/schema.json `levels`): one level —
// nothing to choose; [off, X] — a Thinking switch; more — a list.
const m = c.app.engine.model
const { levels, control } = levelsFor(m)
const on = levels.find((l) => l !== "off")!
const set = (v: string) => {
if (control === "switch" && (v === "on" || v === "thinking")) v = on
if (!(levels as string[]).includes(v)) return c.view.info(control === "switch" ? `${m.ref} has a Thinking switch: /effort on or /effort off` : `${m.ref} offers ${levels.join(", ")}`, "warn")
c.setEffort(v as Level)
c.view.info(control === "switch" ? `thinking ${v === "off" ? "off" : `on (${v})`}` : `effort: ${v}`)
}
// The Subagents setting sits beside the effort (team.md §2, subagents.md §4: shown only where
// subagents are offered).
const agentsRow = c.app.team.team.offered ? [teamRange(c)] : []
const choose = (v: string) => set(v)
if (control === "none" && (arg || !agentsRow.length)) return c.view.info(`${m.ref} has one level (${levels[0]}): there is nothing to choose`)
if (arg) return set(arg)
if (control === "none") return c.openDialog({ title: `Chat settings — ${m.ref}`, options: agentsRow, onSelect: choose })
if (control === "switch")
return c.openDialog({
title: `Thinking — ${m.ref}`,
options: [
{ label: "on", value: on, description: on === "default" ? "the model's own" : on, current: c.app.engine.effort !== "off" },
{ label: "off", value: "off", current: c.app.engine.effort === "off" },
...agentsRow,
],
onSelect: choose,
})
c.openDialog({
title: `Effort — ${m.ref}`,
options: [...levels.map((e) => ({ label: e === "off" ? "off (no thinking)" : e, value: e, current: c.app.engine.effort === e })), ...agentsRow],
onSelect: choose,
})
},
},
{
name: "team",
args: "[size [<min> <max>] | stop [<name>] | open [<name>] | findings <name>]",
description: "subagents: the tree and the room; how many; stop one; their findings",
run(c, arg) {
// The tree and the room as the lead sees them — the person sees everything (team.md §5).
const words = arg.trim().split(/\s+/).filter(Boolean)
if (words[0] === "size" && words.length === 1) return openTeamSize(c)
// Spec 3.4.0 §11: a subagent's session, read-only, live or after it ended.
if (words[0] === "open") {
const t = c.app.team.team
const all = [...t.tree().filter((a) => a.manager !== null), ...t.ended]
const open = (key: string) => {
const a = t.find(key)
if (!a?.jobId) return c.view.info(`no subagent ${key} with a session here — /team lists them`, "warn")
c.enterJob(a.jobId)
}
if (words[1]) return open(words[1])
if (!all.length) return c.view.info("no subagents yet")
return c.openDialog({
title: "A subagent's session — read-only",
options: all.map((a) => ({ label: `${a.name} (${a.id})`, value: a.id, description: `${a.role} · ${TEAM.read.states[a.state]}` })),
onSelect: open,
})
}
if (words[0] === "stop" && words.length === 1) {
const members = c.app.team.team.tree().filter((a) => a.manager !== null)
if (!members.length) return c.view.info("the team has no members")
return c.openDialog({
title: "Stop a member, and everything under it",
options: members.map((a) => ({ label: a.name, value: a.name, description: `${a.role} · ${a.state}` })),
onSelect: (name) => (c.view.info(teamText(c.app, `stop ${name}`)), c.refresh()),
})
}
c.view.info(teamText(c.app, arg))
c.refresh()
},
},
{
name: "mode",
args: `[${MODES.join("|")}]`,
description: "permission mode",
run(c, arg) {
const set = (raw: string) => {
const m = asMode(raw)
if (!m) return c.view.info(`modes: ${MODES.join(", ")}`, "warn")
if (m === "auto")
return c.openDialog({
title: "Auto: nothing will ask. Sure?",
options: [
{ label: "No, stay in " + c.app.engine.mode, value: "no" },
{ label: "Yes — no approvals (the hardline still holds)", value: "yes" },
],
onSelect: (v) => v === "yes" && c.setMode("auto"),
})
c.setMode(m)
}
if (arg) return set(arg)
c.openDialog({
title: "Permission mode",
options: [
{ label: "manual", value: "manual", description: "ask for everything not already allowed" },
{ label: "edit", value: "edit", description: "file changes in the project need no approval" },
{ label: "plan", value: "plan", description: "read and plan only" },
{ label: "auto", value: "auto", description: "nothing asks (hardline still refused)" },
].map((o) => ({ ...o, current: o.value === c.app.engine.mode })),
onSelect: set,
})
},
},
{
name: "plan",
args: "[task | show | clear]",
description: "plan mode — read, investigate, write a plan for approval; with a task, start on it",
run(c, arg) {
if (arg === "show") {
const plan = c.app.engine.plan
return c.view.info(plan ? render(plan) : "no plan in this session — the model writes one with plan_write")
}
if (arg === "clear") {
if (!c.app.engine.plan) return c.view.info("no plan in this session")
c.app.engine.setPlan(null)
return c.view.info("plan cleared")
}
c.setMode("plan")
if (arg) c.send?.(arg)
else c.view.info("plan mode: the model reads and plans, then asks you to approve the plan. shift+tab leaves it.")
},
},
{
name: "plans",
description: "this project's plans, and this session's",
run(c) {
const live = c.app.engine.plan
const dir = c.app.engine.o.toolCtx.planDir
const files = dir && existsSync(dir) ? readdirSync(dir).filter((f) => f.endsWith(".md")).sort().reverse() : []
const here = live ? `this session: ${live.title} (${planTitle(live)}) — /plan show` : "this session has no plan"
if (!files.length) return c.view.info(`${here}\nno plans saved yet — /plan <task> writes one`)
c.view.info(here)
c.openDialog({
title: "Plans",
options: files.map((f) => ({ label: f, value: f, description: readFileSync(join(dir!, f), "utf8").split("\n").find((l) => l.startsWith("#"))?.replace(/^#+\s*/, "") ?? "" })),
onSelect: (f) => c.view.info(`${f}\n\n${readFileSync(join(dir!, f), "utf8")}`),
})
},
},
{
name: "pages",
args: "[n]",
description: "the HTML pages in this session's replies, saved as files to open in a browser; with a number, that one's path",
run(c, arg) {
const id = c.app.engine.sessionId
const list = id ? listPages(id) : []
if (!list.length) return c.view.info("no pages in this session's replies")
const wanted = arg.trim()
if (wanted) {
const one = list.find((p) => String(p.n) === wanted)
return one ? c.view.info(one.path) : c.view.info(`no page ${wanted} here — ${list.map((p) => p.n).join(", ")}`, "warn")
}
c.view.info(list.map((p) => `${p.n} ${p.path}`).join("\n"))
},
},
{
name: "review",
args: "[head | <branch>]",
description: "a read-only review of this session's changes (or since the last commit, or against a branch)",
run(c, arg) {
// The same builder as a /review sent from the web UI (session/commands.ts).
const r = reviewPrompt(c.app, arg)
if ("message" in r) return c.view.info(r.message, r.message.startsWith("nothing") ? "info" : "warn")
// The diff is somebody else's text: an @path in it must not attach a file.
c.sendAs?.(r.prompt, `/review${arg ? " " + arg : ""}`, { mode: "plan", attachFrom: "" })
},
},
{
name: "changelog",
args: "[since]",
description: "update CHANGELOG.md's Unreleased section from the commits since the last tag",
run(c, arg) {
const r = changelogPrompt(c.app, arg)
if ("message" in r) return c.view.info(r.message, "warn")
c.sendAs?.(r.prompt, `/changelog${arg ? " " + arg : ""}`, { attachFrom: "" })
},
},
{
name: "init",
args: "[what to focus on]",
description: "make or update the project's AGENTS.md from what the repository says",
run(c, arg) {
// A command the web UI offers too, so the TUI has it as well.
c.sendAs?.(initPrompt(c.app, arg), `/init${arg ? " " + arg : ""}`, { attachFrom: arg })
},
},
{
name: "release",
args: "[patch|minor|major|x.y.z]",
description: "bump the version, date the CHANGELOG, commit and tag (signed if git signs) — push only if you choose",
run(c, arg) {
if (!c.app.project.gitRoot) return c.view.info("not a git repository", "warn")
const p = planRelease(c.app.project.root, arg)
if ("blocked" in p && !("to" in p)) return c.view.info(`/release: ${p.blocked}`, "warn")
const plan = p as Exclude<typeof p, { blocked: string }>
const what = plan.tagOnly ? [`HEAD is already "Release ${plan.to}" but has no tag: only the tag ${plan.tag} is made`] : [
`${plan.versionFile.file}${plan.versionFile.also?.length ? ` (and ${plan.versionFile.also.map((a) => a.file).join(", ")})` : ""}: ${plan.from} → ${plan.to}`,
plan.changelog ? `CHANGELOG.md: Unreleased → [${plan.to}] (${plan.changelog.notes.split("\n").length} lines)` : "CHANGELOG.md: nothing under Unreleased — the tag will have no notes (/changelog first?)",
`commit "Release ${plan.to}", then ${plan.sign ? "a signed" : "an annotated"} tag ${plan.tag}`,
]
const notes = plan.changelog?.notes ?? plan.notes
c.view.info(`release plan:\n${what.map((w) => ` ${w}`).join("\n")}${notes ? `\n\ntag message:\n${plan.name} ${plan.to}\n\n${notes.slice(0, 1500)}` : ""}`)
c.openDialog({
title: `Release ${plan.tag}?`,
options: [
{ label: "Release", value: "local", description: "commit and tag here; push nothing" },
{ label: "Release and push", value: "push", description: "then push the branch and the tag" },
{ label: "Cancel", value: "no" },
],
onSelect: (v) => {
if (v === "no") return c.view.info("no release made")
const r = doRelease(plan, v === "push")
c.view.info(r.lines.join("\n"), r.ok ? undefined : "warn")
c.refresh()
},
})
},
},
{
name: "tasks",
description: "the project's task board (.agent/tasks.md)",
run(c) {
if (!existsSync(c.app.project.dir)) return c.view.info("no project directory yet — /trust creates it", "warn")
c.view.info(`tasks — ${c.app.project.dir.slice(c.app.project.root.length + 1)}/tasks.md\n\n${renderBoard(readBoard(c.app.project.dir))}`)
},
},
{
name: "decisions",
description: "the project's decision log",
run(c) {
if (!existsSync(c.app.project.dir)) return c.view.info("no project directory yet — /trust creates it", "warn")
const d = readDecisions(c.app.project.dir)
c.view.info(d.length ? d.map((x) => `${x.date} · ${x.title}\n${x.text}`).join("\n\n") : "no decisions recorded — /decide <title>: <decision>")
},
},
{
name: "decide",
args: "<title>: <decision>",
description: "record a decision in the project's log",
run(c, arg) {
if (!existsSync(c.app.project.dir)) return c.view.info("no project directory yet — /trust creates it", "warn")
const m = /^([^:]{3,80}):\s*([\s\S]+)$/.exec(arg.trim())
if (!m) return c.view.info("usage: /decide <title>: <decision> — e.g. /decide Storage: SQLite, one file per project", "warn")
const d = addDecision(c.app.project.dir, m[1]!, m[2]!)
c.view.info(`recorded: ${d.date} · ${d.title}`)
},
},
{
name: "jobs",
args: "[<id> | stop <id>]",
description: "background jobs: the list (enter opens one, k stops it); /jobs <id> opens one, /jobs stop <id> stops it",
run(c, arg) {
const [first, second] = arg.trim().split(/\s+/).filter(Boolean)
if (!first) return openJobs(c)
if (first === "stop") return second ? stopJob(c, second) : c.view.info("/jobs stop <id> — /jobs lists them", "warn")
openJob(c, first)
},
},
{
name: "sessions",
aliases: ["resume"],
args: "[delete]",
description: "resume a session in this project; ctrl+d in the list, or /sessions delete, deletes one (here and in the web UI)",
run(c, arg) {
if (arg.trim() === "delete") {
const list = otherSessions(c)
if (!list.length) return c.view.info("no other sessions in this project")
return c.openDialog({
title: "Delete a session",
options: list.map((s) => sessionOption(c, s)),
onSelect: (id) => confirmDelete(c, id),
})
}
openResume(c)
},
},
{
name: "remote",
args: "[off]",
description: "share this session with the LLeMbas web UI (through the service, or held by this terminal while it runs); off stops it",
run(c, arg) {
if (!c.share) return c.view.info("/remote is not available here", "warn")
const a = arg.trim().toLowerCase()
if (a === "status" || a === "?") return c.view.info(c.share.status())
void c.share.remote(a !== "off").then((m) => (c.view.info(m), c.refresh()))
},
},
{
name: "rename",
args: "<title>",
description: "name this session (here, in /sessions, and in the web UI when it is shared)",
run(c, arg) {
const title = arg.trim().replace(/\s+/g, " ").slice(0, 200)
const id = c.app.engine.sessionId
const now = (id && c.app.store?.session(id)?.title) || ""
if (!id) return c.view.info("nothing to name yet: this session has no messages", "warn")
if (!title) return c.view.info(`usage: /rename <title>${now ? ` — now "${now}"` : ""}`, "warn")
c.app.rename(title)
c.view.info(`named "${title}"`)
c.refresh()
},
},
{
name: "search",
args: "<words> [all]",
description: "find a past session by what was said in it (this project's; add `all` for every project) and resume it",
async run(c, arg) {
const store = c.app.store
if (!store) return c.view.info("sessions are not being recorded here", "warn")
const words = arg.trim().split(/\s+/).filter(Boolean)
const everywhere = words.at(-1) === "all"
const query = (everywhere ? words.slice(0, -1) : words).join(" ")
if (!query) return c.view.info('usage: /search <words> [all] — plain words, "a phrase", OR, NOT', "warn")
const { searchSessions } = await import("../tool/session_search.ts")
let found: ReturnType<typeof searchSessions>
try {
found = searchSessions(store, query, { root: everywhere ? undefined : c.app.project.root, exclude: c.app.engine.sessionId ? [c.app.engine.sessionId] : [] })
} catch (e) {
return c.view.info(`/search: that could not be searched (${(e as Error).message}) — use plain words, "quoted phrases", OR and NOT`, "warn")
}
if (!found.length) return c.view.info(`nothing found for "${query}"${everywhere ? "" : " in this project's sessions — /search " + query + " all looks in every project"}`)
const other = (id: string) => store.session(id)?.root !== c.app.project.root
c.openDialog({
title: `${found.length} session${found.length === 1 ? "" : "s"} for "${query}"`,
width: 100,
options: found.map((f) => ({
label: f.title || firstUserLine(c, f.id) || f.id,
value: f.id,
description: `${new Date(f.updated).toISOString().slice(0, 16).replace("T", " ")}${other(f.id) ? ` · ${store.session(f.id)?.root}` : ""} · ${f.role}: ${f.snippet}`,
})),
onSelect: async (id) => {
if (other(id)) return c.view.info(`that session is in ${store.session(id)?.root} — resume it there (lembas -c, /sessions)`, "warn")
const why = await (c.share?.release(id) ?? Promise.resolve(undefined))
if (why) return c.view.info(`not resumed: ${why}`, "warn")
c.view.load(c.app.resume(id), c.app.engine.storedEnd())
c.view.info(`resumed ${id}`)
},
})
},
},
{
name: "delete",
description: "delete this session (here and in the web UI) and start a new one",
run(c) {
const id = c.app.engine.sessionId
if (!id) return c.view.info("no session to delete", "warn")
confirmDelete(c, id, true)
},
},
{
name: "new",
aliases: ["clear"],
description: "start a new session",
run(c) {
c.app.newSession()
c.view.clear()
},
},
{
name: "copy",
args: "[n]",
description: "copy the last reply (or the nth from the end) as markdown — for when selecting with the mouse cannot reach your clipboard",
run(c, arg) {
const n = Math.max(1, Number.parseInt(arg || "1", 10) || 1)
const replies = c.app.engine.messages
.filter((m) => m.role === "assistant")
.map((m) => (m.role === "assistant" ? m.parts.map((p) => (p.type === "text" ? p.text : "")).join("").trim() : ""))
.filter(Boolean)
const text = replies[replies.length - n]
if (!text) return c.view.info(replies.length ? `there are only ${replies.length} replies` : "no reply to copy yet")
c.copy(text)
},
},
{
name: "undo",
description: "take back the last prompt: its file changes and its conversation",
run(c) {
const r = c.app.turns.undo()
if (!r) return c.view.info("nothing to undo in this session")
c.view.load(c.app.engine.messages)
c.setInput(r.prompt)
c.view.info(r.files.length ? `undone — restored ${r.files.length} file${r.files.length > 1 ? "s" : ""}: ${r.files.join(", ")}` : "undone — no files had changed")
if (!c.app.snapshots?.enabled && c.app.snapshots?.reason) c.view.info(c.app.snapshots.reason, "warn")
},
},
{
name: "redo",
description: "put back what /undo took",
run(c) {
const r = c.app.turns.redo()
if (!r) return c.view.info("nothing to redo")
c.view.load(c.app.engine.messages)
c.setInput("")
c.view.info(r.files.length ? `redone — ${r.files.join(", ")}` : "redone")
},
},
{
name: "diff",
args: "[head]",
description: "what this session changed (or: everything since the last commit)",
run(c, arg) {
if (arg === "head") {
if (!c.app.project.gitRoot) return c.view.info("not a git repository", "warn")
const tracked = git(c.app.project.gitRoot, ["diff", "--no-color", "--no-ext-diff", "--no-textconv", "HEAD"])
const patch = tracked.code === 0 ? tracked.out : git(c.app.project.gitRoot, ["diff", "--no-color", "--no-ext-diff", "--no-textconv"]).out
return patch ? c.view.diff("changes since the last commit", patch) : c.view.info("no changes since the last commit")
}
const patch = c.app.turns.diff()
if (patch === undefined) return c.view.info(c.app.snapshots?.reason || "no prompt yet in this session", "warn")
patch ? c.view.diff("changes in this session", patch) : c.view.info("nothing changed in this session")
},
},
{
name: "checkpoint",
args: "[name]",
description: "save the files as they are now under a name, to come back to later (kept across sessions)",
run(c, arg) {
const snaps = c.app.snapshots
if (!snaps) return c.view.info("checkpoints need snapshots, which are off here", "warn")
const label = arg.trim() || `checkpoint ${new Date().toISOString().slice(0, 16).replace("T", " ")}`
const cp = snaps.checkpoint(label)
if (!cp) return c.view.info(snaps.reason || "the checkpoint could not be saved", "warn")
c.view.info(`checkpoint saved: ${label} — /checkpoints goes back to it`)
},
},
{
name: "checkpoints",
description: "this project's checkpoints: go back to one, see what changed since, or delete it",
run(c) {
const snaps = c.app.snapshots
if (!snaps) return c.view.info("checkpoints need snapshots, which are off here", "warn")
const list = snaps.checkpoints()
if (!list.length) return c.view.info(snaps.reason || "no checkpoints yet — /checkpoint [name] saves one", snaps.reason ? "warn" : undefined)
const when = (ms: number) => new Date(ms).toISOString().slice(0, 16).replace("T", " ")
c.openDialog({
title: "Checkpoints",
options: list.map((cp) => ({ label: cp.label, value: cp.ref, description: when(cp.at) })),
onSelect: (ref) => {
const cp = list.find((x) => x.ref === ref)!
const now = snaps.track()
const files = now ? snaps.changed(cp.tree, now) : []
c.openDialog({
title: `${cp.label} — ${files.length ? `${files.length} file${files.length > 1 ? "s" : ""} differ now` : "nothing has changed since"}`,
options: [
...(files.length
? [
{ label: "Go back to it", value: "restore", description: `put ${files.length > 3 ? `${files.length} files` : files.join(", ")} back as they were; what is there now is saved as a checkpoint first` },
{ label: "Show what changed since", value: "diff" },
]
: []),
{ label: "Delete the checkpoint", value: "drop", description: "the files are not touched" },
{ label: "Cancel", value: "no" },
],
onSelect: (v) => {
if (v === "diff") return now && c.view.diff(`changes since "${cp.label}"`, snaps.diff(cp.tree, now))
if (v === "drop") return c.view.info(snaps.dropCheckpoint(cp.ref) ? `deleted checkpoint: ${cp.label}` : "could not delete it", "warn")
if (v !== "restore") return
const r = snaps.restoreCheckpoint(cp)
if (!r) return c.view.info(snaps.reason || "could not go back to the checkpoint", "warn")
c.view.info(`back at "${cp.label}" — restored ${r.files.length} file${r.files.length === 1 ? "" : "s"}${r.saved ? `; what was there is the checkpoint "${r.saved.label}"` : ""}`)
// The conversation is not rewound: the model has to know its files moved under it.
if (r.files.length) c.app.engine.pendingContext.push(`<note>The user restored the files to the checkpoint "${cp.label}" (${when(cp.at)}). These files changed: ${r.files.join(", ")}. Read a file again before changing it.</note>`)
},
})
},
})
},
},
{
name: "branch",
args: "[name]",
description: "the git branches: switch to one; with a name, switch to it (made from here if it is new)",
run(c, arg) {
const root = c.app.project.gitRoot
if (!root) return c.view.info("not a git repository", "warn")
const go = (name: string) => {
const r = switchBranch(root, name)
c.view.info(r.text, r.ok ? undefined : "warn")
if (r.ok && !r.text.startsWith("already")) c.app.engine.pendingContext.push(`<note>The user switched the repository to the branch ${name}.</note>`)
c.refresh()
}
if (arg.trim()) return go(arg.trim())
const list = branches(root)
if (!list.length) return c.view.info("no branches yet — the repository has no commit; /branch <name> names the first one", "warn")
const when = (ms: number) => new Date(ms).toISOString().slice(0, 10)
c.openDialog({
title: "Branches",
options: [
{ label: "New branch…", value: "\0new", description: "type /branch <name>: made from the current commit, uncommitted changes come along" },
...list.map((b) => ({ label: b.name, value: b.name, current: b.current, description: `${when(b.at)} · ${b.last}${b.upstream ? ` · ${b.upstream}` : ""}` })),
],
onSelect: (name) => {
if (name === "\0new") return c.setInput("/branch ")
const b = list.find((x) => x.name === name)!
if (b.current) return c.view.info(`already on ${name}`)
c.openDialog({
title: name,
options: [
{ label: "Switch to it", value: "switch", description: "uncommitted changes come along when git can carry them" },
{ label: "Delete it", value: "delete", description: "only if its work is merged (git branch -d)" },
{ label: "Cancel", value: "no" },
],
onSelect: (v) => {
if (v === "switch") return go(name)
if (v !== "delete") return
const r = deleteBranch(root, name)
c.view.info(r.text, r.ok ? undefined : "warn")
},
})
},
})
},
},
{
name: "commit",
args: "[message | all]",
description: "commit this session's changes (or what is staged) — the model drafts the message",
async run(c, arg) {
const root = c.app.project.gitRoot
if (!root) return c.view.info("not a git repository — create one with git init", "warn")
const all = arg === "all"
const plan = commitPlan(root, c.app.turns.touched(), all)
if (!plan.files.length) return c.view.info(all ? "nothing to commit" : "nothing this session changed is uncommitted — /commit all takes every change", "warn")
const confirm = (message: string) =>
c.openDialog({
title: `Commit ${plan.files.length} file${plan.files.length > 1 ? "s" : ""}${plan.staged ? " (as staged)" : ""}?`,
options: [
{ label: "Commit", value: "yes", description: message.split("\n")[0] },
{ label: "Cancel", value: "no", description: "type /commit <your message> to write it yourself" },
],
onSelect: (v) => {
if (v !== "yes") return c.view.info("not committed")
const r = commit(root, plan, message)
r.ok ? c.view.info(`committed ${r.text}`) : c.view.info(`commit failed: ${r.text}`, "warn")
c.refresh()
},
})
if (arg && !all) return confirm(arg)
c.view.info(`drafting a message for: ${plan.files.join(", ")}`)
try {
const message = await draftMessage(c.app.small().client, root, plan)
if (!message) return c.view.info("the model returned no message; /commit <message> writes it yourself", "warn")
c.view.info(`proposed message:\n\n${message}`)
confirm(message)
} catch (e) {
c.view.info(`could not draft a message: ${(e as Error).message}`, "warn")
}
},
},
{
name: "context",
args: "[<subagent>]",
description: "where the context window goes — the session's, or a subagent's",
run(c, arg) {
// Spec 3.4.0 §12: a subagent's context, by its name or id.
const who = arg.trim()
if (who) {
const t = c.app.team.team
const a = t.find(who)
if (!a) return c.view.info(`no subagent ${who} — /team lists them`, "warn")
const h = c.app.team.handle(a.name)
if (!h || a.endState) {
const u = a.context
return c.view.info(u ? `${a.name} (${a.id}): ${u.used} of ${u.window} tokens (${Math.floor((u.used * 100) / u.window)}%) when it ${a.endState ? TEAM.read.states[a.state] : "last said"}` : `${a.name} (${a.id}): no context use known yet`)
}
const se = h.engine
const sys = se.systemPrompt()
return c.view.info(`${a.name} (${a.id}) — ${a.role}\n${renderBreakdown(contextBreakdown(se, sys, [], undefined, se.o.tools))}`)
}
const e = c.app.engine
// The prompt the session sends (made now if no request has gone yet).
const system = e.systemPrompt()
const git = /<git>\n([\s\S]*?)\n<\/git>/.exec(system)?.[1]
c.view.info(renderBreakdown(contextBreakdown(e, system, instructionFiles(e.o.toolCtx.cwd, c.app.project.root, c.app.loaded.instructions), git, e.o.tools)))
},
},
{
name: "compact",
description: "summarise the conversation to free context",
async run(c) {
c.view.info("compacting…")
try {
const summary = await c.app.engine.compact()
c.view.clear()
c.view.info(`compacted — the model continues from this summary:\n\n${summary}`)
} catch (e) {
c.view.info(`compaction failed: ${(e as Error).message}`, "warn")
}
},
},
{
name: "memory",
description: "what is remembered across sessions — the server's memory when it keeps it, and this machine's files — pick an entry to change or forget it",
async run(c) {
const stores = [
{ key: "user", title: "user", store: c.app.memory, target: "user" as const },
{ key: "memory", title: "notes", store: c.app.memory, target: "memory" as const },
...(c.app.projectMemory ? [{ key: "project", title: "project", store: c.app.projectMemory, target: "memory" as const }] : []),
]
// library: lembas — the account's memory on the server (harness acp.md *Memory*), read
// through its /mcp as the prompt reads it, and changed with its memory tool.
const { LIBRARY_SERVER } = await import("../mcp/index.ts")
const { MEMORY_RESOURCE } = await import("../app.ts")
const onServer = c.app.mcp.servers.has(LIBRARY_SERVER)
const scope = c.app.engine.o.toolCtx.libraryScope
let server: string[] = []
let serverError = ""
if (onServer) {
try {
server = serverEntries(await c.app.mcp.readResource(LIBRARY_SERVER, MEMORY_RESOURCE, scope))
} catch (e) {
serverError = (e as Error).message
}
}
type Entry = { title: string; text: string; value: string; change: (op: { action: "replace" | "remove"; content?: string }) => Promise<string> | string }
const local: Entry[] = stores.flatMap((s) =>
s.store.entries(s.target).map((text, i) => ({
title: s.title,
text,
value: `${s.key}:${i}`,
change: (op: { action: "replace" | "remove"; content?: string }) => {
const r = s.store.apply(s.target, [{ ...op, old_text: text }])
if (!r.ok) throw new Error(r.message)
return r.usage
},
})),
)
const remote: Entry[] = server.map((text, i) => ({
title: "server",
text,
value: `server:${i}`,
change: (op: { action: "replace" | "remove"; content?: string }) => c.app.mcp.callText(LIBRARY_SERVER, "memory", { target: "user", action: op.action, old_text: text, ...(op.content !== undefined ? { content: op.content } : {}) }, scope),
}))
const entries = [...remote, ...local]
const files = stores.map((s) => `${s.title}: ${s.store.file(s.target)} (${s.store.usage(s.target)})`).join("\n")
const where = onServer ? `the server's memory (${serverError ? `could not be read: ${serverError}` : `${server.length} entr${server.length === 1 ? "y" : "ies"}`}) is what sessions here use; this machine's files:\n${files}` : `the files, which you can also edit by hand (entries are separated by a line with §):\n${files}`
if (!entries.length) return c.view.info(`nothing remembered yet — the agent saves facts with its memory tool, or tell it "remember that …"\n${where}`)
const change = async (e: Entry, op: { action: "replace" | "remove"; content?: string }) => {
try {
const said = await e.change(op)
c.view.info(`${e.title}: ${op.action === "remove" ? "forgotten" : "changed"} — the agent sees it from the next session${said && e.title !== "server" ? ` (${said})` : ""}`)
} catch (err) {
c.view.info(`${e.title}: ${(err as Error).message}`, "warn")
}
}
c.openDialog({
title: "Memory — enter to change or forget",
filter: true,
options: entries.map((e) => ({ label: `[${e.title}] ${e.text.replace(/\s+/g, " ").slice(0, 90)}`, value: e.value })),
onSelect: (v) => {
const e = entries.find((x) => x.value === v)!
c.openDialog({
title: e.text.replace(/\s+/g, " ").slice(0, 60),
options: [
{ label: "Forget it", value: "remove" },
{ label: "Keep it", value: "keep" },
],
onSelect: (a) => void (a === "remove" && change(e, { action: "remove" })),
custom: { label: "replace with", onSubmit: (text) => void change(e, { action: "replace", content: text }) },
})
},
})
c.view.info(where)
},
},
{
name: "skills",
description: "list the skills; pick one to use it",
run(c) {
const all = c.app.skills()
if (!all.length) return c.view.info(`no skills yet — put a <name>/SKILL.md in ~/.config/lembas/skills/, or ask the agent to save one`)
c.openDialog({
title: "Skills",
options: all.map((s) => ({ label: `${s.name}${s.category ? ` · ${s.category}` : ""}${s.source === "project" ? " · project" : ""}`, value: s.name, description: s.description })),
// Through /skill: a skill named like a built-in (review, plan…) would run that instead.
onSelect: (name) => c.setInput(`/skill ${slug(name)} `),
})
},
},
{
name: "skill",
args: "<name> [instruction]",
description: "use a skill (for one whose name a command already has)",
run(c, arg) {
const [name = "", ...rest] = arg.trim().split(/\s+/)
const s = findSkill(c.app.skills(), name)
if (!s) return c.view.info(name ? `no skill "${name}" — /skills lists them` : "usage: /skill <name> [instruction]", "warn")
c.sendAs?.(skillMessage(s, rest.join(" ")), `/skill ${arg.trim()}`, { attachFrom: rest.join(" ") })
},
},
{
name: "command",
args: "<name> [words]",
description: "run a custom command by its name (for one whose name a built-in command has)",
run(c, arg) {
const [name = "", ...rest] = arg.trim().split(/\s+/)
if (!name) return c.view.info("usage: /command <name> [words] — a custom command from .agent/commands or ~/.config/lembas/commands", "warn")
if (!c.runCustom(name, rest.join(" "), `/command ${arg.trim()}`)) c.view.info(`no custom command "${name}"`, "warn")
},
},
{
name: "personality",
args: "[preset|custom|none]",
description: "how the agent talks: a preset, your own (personality_custom), or none (remembered)",
run(c, arg) {
// The presets the web UI has; saved to the global config, so every project and session has it.
const set = (name: string) => {
try {
c.app.settings.set("personality", name, "global")
const now = c.app.loaded.config.personality
c.view.info(now ? `personality: ${now}` : "personality: none")
if (now === "custom" && !c.app.loaded.config.personality_custom?.trim())
c.view.info("personality_custom is empty, so custom says nothing yet — /settings personality_custom sets it", "warn")
} catch (e) {
c.view.info((e as Error).message, "warn")
}
c.refresh()
}
if (arg.trim()) return set(arg.trim().toLowerCase())
const now = c.app.loaded.config.personality || "none"
c.openDialog({
title: "Personality",
options: ["none", ...PERSONALITIES].map((n) => ({
label: n,
value: n,
current: n === now,
description: n === "none" ? "just the agent" : n === "custom" ? "your own words: personality_custom" : (personalityText(n) ?? "").slice(0, 70),
})),
onSelect: set,
})
},
},
{
name: "voice",
args: "[test]",
description: "voice input and output: what is set up; test speaks a sentence",
async run(c, arg) {
const v = c.app.loaded.config.voice
if (arg.trim() === "test") {
if (!c.voice.canSpeak) return c.view.info("voice output is not set up (voice.tts)", "warn")
c.view.info("speaking a test sentence — esc stops it")
return void c.voice.speak("This is LLeMbas. If you can hear this, **voice output works**.")
}
const rec = recorderFor(v)
const play = playerFor(v)
const key = keyMatcher(v?.record_key)
const lines = [
`input: ${v?.stt ? `${v.stt.provider ?? "openai"} · ${v.stt.base_url ?? "?"}${v.stt.model ? ` · ${v.stt.model}` : ""}${v.stt.language ? ` · ${v.stt.language}` : ""}` : "not set up (voice.stt)"}`,
`output: ${v?.tts ? `${v.tts.provider ?? "openai"} · ${v.tts.provider === "piper-cli" ? (v.tts.model_path ?? "?") : (v.tts.base_url ?? "?")}${v.tts.voice ? ` · ${v.tts.voice}` : ""}` : "not set up (voice.tts)"}`,
`record: ${typeof rec === "string" ? `✗ ${rec}` : rec.join(" ")}`,
`play: ${typeof play === "string" ? `✗ ${play}` : `${play.join(" ")} — the program that plays speech (${PLAYER_NAMES[play[0]!] ?? "an audio player"})`}`,
`key: ${key.label} ${v?.record_mode === "hold" ? "(hold, or press again)" : "(press to start, again — or silence — to stop)"} · esc cancels`,
`what you say goes ${v?.submit === "send" ? "straight to the model" : "into the prompt, to check and send"}`,
...(v?.tts
? [
`replies are ${c.voice.speakReplies ? "spoken" : "not spoken"} — /speak turns reading replies aloud ${c.voice.speakReplies ? "off" : "on"}; /voice test says one sentence now;`,
` voice.speak: true in config.yaml speaks from the start · esc or ${key.label} stops speech`,
]
: []),
]
c.view.info(lines.join("\n") + (v ? "" : "\n\nSet it up under voice: in ~/.config/lembas/config.yaml — https://llembas.eu/docs/ has an example."))
},
},
{
name: "speak",
args: "[on|off]",
description: "read replies aloud (this session)",
run(c, arg) {
const a = arg.trim()
if (!c.voice.canSpeak) return c.view.info("voice output is not set up (voice.tts) — /voice shows what is", "warn")
c.voice.speakReplies = a === "on" ? true : a === "off" ? false : !c.voice.speakReplies
if (!c.voice.speakReplies) c.voice.stopSpeaking()
c.view.info(c.voice.speakReplies ? "replies are spoken: each one is read aloud as it arrives — esc stops it, /speak again turns it off" : "replies are not spoken")
c.refresh()
},
},
{
name: "mcp",
args: "[<server> | reconnect|on|off|auth|code|logout <server>]",
description: "MCP servers: a menu of them — state, tools and what each may do, sign in, switch one on or off",
async run(c, arg) {
const mcp = c.app.mcp
const [sub = "", name = "", ...rest] = arg.trim().split(/\s+/)
// No word: the menu. A server's name: that server.
if (!sub) return openMcpMenu(c)
if (mcp.servers.has(sub) && !name) return openMcpServer(c, sub)
if (!["reconnect", "on", "off", "auth", "code", "logout"].includes(sub)) return c.view.info(`no MCP server "${sub}" — ${[...mcp.servers.keys()].join(", ") || "none are configured"}`, "warn")
if (!name || !mcp.servers.has(name)) return c.view.info(`usage: /mcp ${sub} <server> — servers: ${[...mcp.servers.keys()].join(", ") || "(none)"}`, "warn")
await runAction(c, name, sub, { menu: false, code: rest.join(" ") })
},
},
{
name: "login",
args: "[address]",
description: "sign in to a LLeMbas instance: its models, voice and search (again: read them now)",
async run(c, arg) {
const { codeInstructions, instances, isWebuiEntry, login, loginSummary, sync } = await import("../lembas/login.ts")
const known = instances()
const go = async (address: string) => {
const again = Object.values(known).find((i) => i.base_url.replace(/^https?:\/\//, "") === address.trim().replace(/^https?:\/\//, "").replace(/\/+$/, ""))
try {
if (again && isWebuiEntry(again.connection)) {
c.view.info(`${again.connection}: reading the models from ${again.base_url}…`)
const r = await sync(again.connection)
c.view.info(`${loginSummary(r, false).join("\n")}\n\nStarting again to load them.`)
return c.reload()
}
c.view.info(`Looking for LLeMbas at ${address}…`)
const r = await login(address, { ca: again?.ca, onCode: (start, d) => c.view.info(codeInstructions(start, d)) })
c.view.info(`${loginSummary(r, true).join("\n")}\n\nStarting again to load them.`)
c.reload()
} catch (e) {
c.view.info(`login: ${(e as Error).message}`, "warn")
}
}
if (arg.trim()) return go(arg.trim())
c.openDialog({
title: "Log in to a LLeMbas instance",
options: Object.values(known).map((i) => ({ label: i.base_url, value: i.base_url, description: `refresh ${i.connection}${i.email ? ` · ${i.email}` : ""}` })),
custom: { label: "Log in to", onSubmit: (text) => void go(text) },
onSelect: (value) => void go(value),
})
},
},
{
name: "usage",
description: "your account's usage on the LLeMbas instance: this month's tokens and cost, this device's tokens",
async run(c) {
// /v1/usage of the account this machine is logged in to.
const { accountUsage, spendLabel, usageLines } = await import("../lembas/personal.ts")
try {
const r = await accountUsage()
if (!r) return c.view.info("no usage to show — not logged in to one LLeMbas instance (/login), or it does not keep any")
c.setSpend?.(spendLabel(r.usage))
c.view.info(usageLines(r.connection, r.usage).join("\n"))
} catch (e) {
c.view.info(`/usage: ${(e as Error).message}`, "warn")
}
},
},
{
name: "schedule",
aliases: ["schedules"],
args: "[list | new <words> | pause|resume|run|history <id or title>]",
description: "your schedules on the LLeMbas server: list, create from words, pause, resume, run now, history",
async run(c, arg) {
const S = await import("../lembas/schedules.ts")
const [sub = "list", ...rest] = arg.trim().split(/\s+/).filter(Boolean)
const words = arg.trim().slice(sub.length).trim()
const say = (lines: string[], level?: "warn") => c.view.info(lines.join("\n"), level)
try {
if (sub === "list" || sub === "ls") return say(S.listLines(await S.listSchedules()))
if (sub === "new" || sub === "create" || sub === "add") {
if (!words) return say(["usage: /schedule new <what, and when> — e.g. /schedule new every weekday at 8:30 summarise the open issues"], "warn")
c.view.info("working out what that means…")
const compiled = await S.compileWords(words)
const preview = S.previewLines(compiled)
if (!Object.keys(compiled.rule ?? {}).length) return say([...preview, "", "No schedule made: say when it should run (\"every Monday at 9\", \"tomorrow at 7\")."], "warn")
const cwd = c.app.engine.o.toolCtx.cwd
const { coveredBy, remoteState } = await import("../config/remote.ts")
const here = S.canRunHere() && coveredBy(cwd, remoteState().roots) !== undefined
c.openDialog({
title: preview[0]!,
options: [
// The two types (harness acp.md, spec 3.9.0). An Agentic one names no model: each run
// uses the model last used in this directory.
{ label: "Create it — Chat", value: "server", description: preview.slice(1).join(" · ") || "runs on the server" },
...(here ? [{ label: "Create it — Agentic, to run on this computer", value: "here", description: `as an agent turn in ${cwd}, in Edit, on the model last used here` }] : []),
{ label: "Cancel", value: "cancel" },
],
onSelect: (v) => {
if (v === "cancel") return c.view.info("no schedule made")
// Then: when to hear from it (harness reports.md §3) — every run, when something
// the person names holds, or never. Typing gives the condition in their own words.
const N = LOOP.scheduled.notify
const create = (notify: string, condition?: string) =>
void S.createSchedule(compiled, words, v === "here" ? { cwd } : undefined, { notify, condition })
.then((made) => c.view.info(`made ${made.title} (${made.id}) — next ${S.when(made.next_run)} · ${S.notifyWords(notify, condition)}`, "success"))
.catch((e) => c.view.info(`/schedule: ${(e as Error).message}`, "warn"))
c.openDialog({
title: "Notify when — when should it file a report?",
body: ["A run that files no report leaves one line in the schedule's history. Type your own condition to say when a report is wanted."],
options: [
{ label: `When ${N.default_condition}`, value: "when", current: true, description: "the model decides" },
{ label: N.labels.always, value: "always", description: "a report every time, also when nothing was found" },
{ label: N.labels.never, value: "never", description: "no reports: the history only" },
],
custom: { label: "When", onSubmit: (condition) => create("when", condition) },
onSelect: (n) => create(n),
})
},
})
return
}
if (["pause", "resume", "run", "history", "runs"].includes(sub)) {
const found = S.findSchedule(await S.listSchedules(), rest.join(" "))
if (typeof found === "string") return say([found], "warn")
if (sub === "pause") return say([`paused ${(await S.pauseSchedule(found.id)).title}`])
if (sub === "resume") {
const s = await S.resumeSchedule(found.id)
return say([`resumed ${s.title} — next ${S.when(s.next_run)}`])
}
if (sub === "run") {
const r = await S.runSchedule(found.id)
return say([`${found.title}: started now (${r.id}) — the next scheduled run stays as it was; /schedule history ${found.id}`])
}
const h = await S.scheduleRuns(found.id)
return say(S.historyLines(found, h.runs, h.more))
}
say([`usage: /schedule ${COMMANDS.find((x) => x.name === "schedule")!.args}`], "warn")
} catch (e) {
say([`/schedule: ${(e as Error).message}`], "warn")
}
},
},
{
name: "reports",
args: "[<id or title>]",
description: "the reports your schedules filed: unread first — read one, reply, mark it read, delete it",
async run(c, arg) {
if (!arg.trim()) return openReports(c)
// A name: that report, read here.
const R = await import("../lembas/reports.ts")
try {
const found = R.findReport(await R.listReports(), arg)
if (typeof found === "string") return c.view.info(found, "warn")
await readReport(c, found.id)
} catch (e) {
c.view.info(`/reports: ${(e as Error).message}`, "warn")
}
},
},
{
name: "logout",
args: "[connection]",
description: "sign out of a LLeMbas instance, and remove the connection login wrote",
async run(c, arg) {
const { instances, logout } = await import("../lembas/login.ts")
const known = Object.keys(instances())
const go = async (name: string) => {
try {
const r = await logout(name)
c.view.info(`Signed out of ${name}${r.revoked ? "" : " here — the instance could not be told; revoke it under User → Security → Devices there"}.${r.modelLeft ? ` config.yaml still starts on ${r.modelLeft}; /model picks another.` : ""}`)
c.reload()
} catch (e) {
c.view.info(`logout: ${(e as Error).message}`, "warn")
}
}
if (arg.trim()) return go(arg.trim())
if (!known.length) return c.view.info("not logged in to any LLeMbas instance — /login <address>")
if (known.length === 1) return go(known[0]!)
c.openDialog({ title: "Sign out of", options: known.map((k) => ({ label: k, value: k })), onSelect: (v) => void go(v) })
},
},
{
name: "settings",
args: "[key [value [session|global|project]]]",
description: "see and change settings — this session, the global config or the project's",
run(c, arg) {
const S = c.app.settings
const set = (key: string, value: string, scope: Scope) => {
try {
const r = S.set(key, value, scope)
c.view.info(r.message)
c.flash(`${key} = ${show(r.value)}`)
} catch (e) {
c.view.info((e as Error).message, "warn")
}
}
const askScope = (key: string, value: string) => {
const def = findSetting(key)!
const scopes = def.scopes.filter((sc) => sc !== "project" || c.app.trusted)
if (scopes.length === 1) return set(key, value, scopes[0]!)
c.openDialog({
title: `${key} = ${value} — where?`,
options: scopes.map((sc) => ({ label: sc, value: sc, description: sc === "session" ? "this session only" : sc === "global" ? "~/.config/lembas/config.yaml" : ".agent/config.yaml" })),
onSelect: (sc) => set(key, value, sc as Scope),
})
}
const askValue = (key: string) => {
const e = S.get(key)
if (e.note) c.view.info(e.note, "warn")
const values =
e.def.kind === "model"
? c.app.modelRefs()
: e.def.kind === "effort"
? [...levelsFor(c.app.engine.model).levels]
: e.def.kind === "boolean"
? ["true", "false"]
: key === "theme"
? themeNames()
: e.def.kind === "enum"
? [...(e.def.values ?? [])]
: []
c.openDialog({
title: `${key} — now ${show(e.value) || e.def.fallback || "unset"}${e.source === "default" ? "" : ` (${e.source})`}${e.def.hint ? ` · ${e.def.hint}` : ""}`,
options: values.map((v) => ({ label: v, value: v, current: v === show(e.value) })),
onSelect: (v) => askScope(key, v),
custom: e.def.kind === "enum" || e.def.kind === "boolean" || e.def.kind === "effort" ? undefined : { label: "set to", onSubmit: (v) => askScope(key, v) },
})
}
const [key, value, scope] = arg.split(/\s+/).filter(Boolean)
if (key && !findSetting(key)) return c.view.info(`no setting ${key} — /settings lists them`, "warn")
if (key && value) return scope ? set(key, value, scope as Scope) : askScope(key, value)
if (key) return askValue(key)
c.openDialog({
title: "Settings",
filter: true,
options: S.list().map((e) => ({
label: e.def.key,
value: e.def.key,
description: `${show(e.value) || e.def.fallback || "unset"}${e.source === "default" ? "" : ` · ${e.source}`}${e.def.reload ? " · after restart" : ""}${e.def.hint ? ` · ${e.def.hint}` : ""}`,
})),
onSelect: askValue,
})
},
},
{
name: "kb",
args: "[new <name> | add <name> <path|url>… | search <query>]",
description: "knowledge bases the agent searches: list, make one, add files, directories or pages",
async run(c, arg) {
const lib = c.app.library
const [verb, name, ...rest] = arg.split(/\s+/).filter(Boolean)
try {
if (verb === "new" && name) {
lib.createBase(name, rest.join(" "))
return c.view.info(`made ${name} — /kb add ${name} <files, directories, urls>`)
}
if (verb === "add" && name && rest.length) {
c.view.info(`adding to ${name}…`)
const added = await ingest(lib, name, rest, { cwd: c.app.project.root })
const bad = added.filter((a) => a.error)
c.view.info(`${name}: ${added.filter((a) => a.changed).length} added or changed, ${added.filter((a) => !a.changed && !a.error).length} unchanged${bad.length ? `, ${bad.length} with no text (${bad.slice(0, 3).map((a) => `${a.source.split("/").pop()}: ${a.error}`).join("; ")})` : ""}`)
if (c.app.embedder) {
const n = await lib.embedAll(c.app.embedder).catch((e) => (c.view.info(`embedding failed: ${(e as Error).message} — searching by words`, "warn"), 0))
if (n) c.view.info(`embedded ${n} pieces with ${c.app.embedder.model}`)
}
return
}
if (verb === "search" && name) {
const q = [name, ...rest].join(" ")
const hits = await lib.search(q, { bases: c.app.engine.o.toolCtx.library?.bases, embedder: c.app.embedder }).catch(() => lib.search(q, { bases: c.app.engine.o.toolCtx.library?.bases }))
return c.view.info(hits.length ? hits.map((h) => `[${h.id}] ${h.title} (${h.base})\n ${h.snippet.replace(/\s+/g, " ").slice(0, 200)}`).join("\n") : "nothing found")
}
if (verb) return c.view.info("/kb, /kb new <name>, /kb add <name> <paths or urls>, /kb search <query>", "warn")
const all = lib.bases()
c.view.info(
all.length
? `${all.map((b) => `${b.name} — ${b.documents} document${b.documents === 1 ? "" : "s"}${b.description ? `: ${b.description}` : ""}`).join("\n")}\nsearched by ${c.app.embedder ? `words and meaning (${c.app.embedder.model})` : "words"}; the agent uses knowledge_search and knowledge_get. lembas kb in a shell does the rest.`
: "no knowledge bases yet — /kb new <name>, then /kb add <name> <files, directories, urls>",
)
} catch (e) {
c.view.info((e as Error).message, "warn")
}
},
},
{
name: "theme",
aliases: ["skin"],
args: "[name]",
description: "colours: lembas, lembas-light, moria, shire, hermes, mono, or your own",
run(c, arg) {
if (arg) return c.setTheme(arg)
c.openDialog({ title: "Theme", options: themeNames().map((n) => ({ label: n, value: n, current: n === c.theme().name })), onSelect: (n) => c.setTheme(n) })
},
},
{
name: "terminal",
description: "what LLeMbas CLI found out about this terminal: synchronized output, clipboard, keyboard, frame rate",
run(c) {
const r = c.renderer
const caps = r.capabilities
const rate = `frame rate: ${r.targetFps}/s (at most ${r.maxFps}) · mouse: ${r.useMouse ? "LLeMbas CLI's (hold Shift for the terminal's own selection)" : "the terminal's"}`
if (!caps) return c.view.info(`the terminal has not answered LLeMbas CLI's questions (yet), so it is treated as having none of the extras\n${rate}`)
const yes = (b: boolean | undefined) => (b ? "yes" : "no")
const t = caps.terminal as { name?: string; version?: string } | undefined
c.view.info(
[
`terminal: ${t?.name ? `${t.name}${t.version ? ` ${t.version}` : ""}` : "did not say"}${caps.remote ? " · over SSH" : ""}`,
`synchronized output: ${yes(caps.sync)}${caps.sync ? "" : " — frames are painted as they arrive, so fast scrolling can tear; drawing at most 12 frames a second"}`,
rate,
`clipboard (OSC 52): ${caps.osc52_support}`,
`kitty keyboard: ${yes(caps.kitty_keyboard)} · true colour: ${yes(caps.rgb)}`,
].join("\n"),
)
},
},
{
name: "icons",
args: "[emoji|plain]",
description: "pictures as emoji, or plain symbols for a terminal font without emoji (boxes with codes like 01F3A4 in them)",
run(c, arg) {
const set = (v: string) => {
if (v !== "emoji" && v !== "plain") return c.view.info("/icons emoji or /icons plain", "warn")
c.setIcons(v)
c.view.info(`icons: ${v} (saved to config.yaml)`)
}
if (arg) return set(arg.trim())
c.openDialog({
title: "Icons",
options: [
{ label: "emoji — 🎤 🔊 📖 💻", value: "emoji", current: c.theme().icons.mic === "🎤" },
{ label: "plain — mic ♪ » $ (for a font without emoji)", value: "plain", current: c.theme().icons.mic !== "🎤" },
],
onSelect: set,
})
},
},
{
name: "trust",
description: "trust this project (its .agent config is then honoured — restart to apply)",
run(c) {
setTrust(c.app.project.root, "trusted")
c.view.info(`${c.app.project.root} is trusted. Restart lembas to load its config.`)
},
},
{
name: "reload",
aliases: ["restart"],
description: "start again on this session — the installed binary (after an update) and every config file read afresh",
run: (c) => c.reload(),
},
{ name: "quit", aliases: ["exit", "q"], description: "leave", run: (c) => c.quit() },
]
/** `text` cut to `width` columns (wide characters counted as the terminal draws them), an ellipsis
* where it was cut — at the end, not the middle: a description is a sentence, not a path. */
export function fitColumns(text: string, width: number): string {
if (Bun.stringWidth(text) <= width) return text
let out = ""
for (const ch of text) {
if (Bun.stringWidth(out + ch) > width - 1) break
out += ch
}
return `${out}…`
}
/** One row of the Jobs picker, exactly `width` columns: icon, id (6), what it is (the column that
* gives), state (7), how long (6), and `!` for one that ended unseen or holds unread output. The
* icon's width is the set's — every emoji here is two columns, every plain icon one — so the
* columns line up by construction. */
export function jobRow(j: JobInfo, width: number, icon: string, unseen = false): string {
const fixed = Bun.stringWidth(icon) + 1 + 6 + 1 + 1 + 7 + 1 + 6 + 1 + 1
const descW = Math.max(4, width - fixed)
const what = `${j.kind === "subagent" && j.agent ? `${j.agent}: ` : ""}${j.description.replace(/\s+/g, " ")}`
const desc = fitColumns(what, descW)
const pad = " ".repeat(Math.max(0, descW - Bun.stringWidth(desc)))
const took = duration((j.ended ?? Date.now()) - j.started, true).padStart(6)
const mark = unseen || j.unread > 0 ? "!" : " "
return `${icon} ${j.id.padEnd(6)} ${desc}${pad} ${j.state.padEnd(7)} ${took} ${mark}`
}
/** The Jobs picker: one row per job of the session, running first. Enter opens one (a command's
* output, or the subagent itself), k stops it, esc closes. The list is a snapshot, so while it is
* shown it redraws itself on the session's job events — at most four times a second — and the
* cursor stays where it was (the dialog is the same one; only its rows change). */
function openJobs(c: CommandCtx) {
if (!c.app.jobs().length) return c.view.info("no jobs in this session — the model starts one with bash background: true or task background: true, and a long command moves there at its timeout (or ctrl+b)")
c.view.seenJobs()
const width = () => Math.min(100, c.dims().width - 4)
const icon = (j: Job) => c.theme().toolEmoji[j.kind === "command" ? "bash" : "task"] ?? c.theme().icons.other
// The dialog draws two columns before the label (the current marker), and its border and padding.
const rows = () => c.app.jobs().map((j) => ({ label: jobRow(c.app.registry.info(j), width() - 6, icon(j), c.view.state.jobs.find((x) => x.id === j.id)?.unseen), value: j.id }))
let open = true
let timer: ReturnType<typeof setTimeout> | undefined
const onClose = () => {
open = false
off()
clearTimeout(timer)
}
const spec = (): DialogSpec => ({
title: "Jobs",
width: width(),
filter: true,
options: rows(),
hint: "↑↓ move · enter open · k stop · esc close · type to filter",
keys: [{ when: (k) => k.name === "k" && !k.ctrl && !k.meta, run: (id) => stopJob(c, id, () => openJobs(c)) }],
onSelect: (id) => openJob(c, id),
onClose,
})
const off = c.app.bus.on((e) => {
if (e.type !== "job" || !open || timer) return
timer = setTimeout(() => {
timer = undefined
if (open) c.openDialog(spec())
}, 250)
})
c.openDialog(spec())
}
/** Open one job: a command's live output, or the subagent itself. */
function openJob(c: CommandCtx, id: string) {
const job = c.app.registry.get(c.app.jobsSessionId(), id)
if (!job) return c.view.info(`no job ${id} in this session — /jobs lists them`, "warn")
if (job.kind === "command") c.openJob(id)
else c.enterJob(id)
}
/** Stop one job. A running subagent is asked about first (its report so far becomes its end note);
* a command is stopped at once. `then`: what follows either way — the picker comes back. */
function stopJob(c: CommandCtx, id: string, then?: () => void) {
const job = c.app.registry.get(c.app.jobsSessionId(), id)
if (!job) return c.view.info(`no job ${id} in this session — /jobs lists them`, "warn")
if (job.state !== "running") {
c.view.info(`${id} is not running (${job.state})`)
return then?.()
}
const stop = () => {
c.app.registry.stop(job)
c.flash(`stopping ${id}`)
}
if (job.kind !== "subagent") {
stop()
return then?.()
}
c.openDialog({
title: `Stop ${id} (${job.agent}: ${fitColumns(job.description, 40)})?`,
options: [
{ label: "Stop", value: "yes", description: "what it has so far goes to the model as its end note" },
{ label: "Cancel", value: "no" },
],
onSelect: (v) => {
if (v === "yes") stop()
then?.()
},
})
}
function firstUserLine(c: CommandCtx, id: string): string {
const m = c.app.store?.messages(id).find((x) => x.role === "user")
if (!m || m.role !== "user") return ""
const t = m.parts.find((p) => p.type === "text")
return t && t.type === "text" ? t.text.split("\n")[0]!.slice(0, 60) : ""
}
export function findCommand(name: string): Command | undefined {
// In a LLeMbas terminal only, and in no menu: /terminal-context, which the terminal types
// (src/terminal/command.ts).
return COMMANDS.find((c) => c.name === name || c.aliases?.includes(name)) ?? (terminalCommands().find((c) => c.name === name) as Command | undefined)
}
/** The sessions /sessions offers: this project's, newest first, not the one open here — and only
* those somebody said something in, as `lembas sessions` and the web UI list them: every
* start and /new makes one, and the empty ones crowded out the rest as rows named by their id. */
/** The server's memory block (`lembas://memory`) as entries: each `- ` line starts one, and the
* lines under it belong to it; the block's own "(…N more" line is not an entry. */
export function serverEntries(block: string): string[] {
const out: string[] = []
for (const line of block.split("\n")) {
if (line.startsWith("- ")) {
if (/^- \(…\d+ more/.test(line)) continue
out.push(line.slice(2))
} else if (out.length && line.trim()) out[out.length - 1] += `\n${line}`
}
return out.map((e) => e.trim()).filter(Boolean)
}
function otherSessions(c: CommandCtx) {
return c.app.store?.sessions(40, c.app.project.root, true).filter((s) => s.id !== c.app.engine.sessionId) ?? []
}
function sessionOption(c: CommandCtx, s: { id: string; title: string; updated: number; model: string }) {
return { label: s.title || firstUserLine(c, s.id) || s.id, value: s.id, description: `${new Date(s.updated).toISOString().slice(0, 16).replace("T", " ")} · ${s.model}` }
}
/** The Resume picker. ctrl+d or Delete deletes the session under the cursor, after asking, and the
* picker comes back without it — so a session can be deleted where it is seen, not only with
* `/sessions delete`. */
function openResume(c: CommandCtx) {
const list = otherSessions(c)
if (!list.length) return c.view.info("no other sessions in this project")
c.openDialog({
title: "Resume",
options: list.map((s) => sessionOption(c, s)),
hint: "↑↓ move · enter resume · ctrl+d delete · esc close",
keys: [{ when: (k) => (k.ctrl === true && k.name === "d") || k.name === "delete", run: (id) => confirmDelete(c, id, false, () => openResume(c)) }],
onSelect: async (id) => {
// A session the web UI runs here (the service) is let go of first, so it is carried on
// rather than forked — or it says why it cannot be, now.
const why = await (c.share?.release(id) ?? Promise.resolve(undefined))
if (why) return c.view.info(`not resumed: ${why}`, "warn")
const shown = c.app.resume(id)
c.view.load(shown)
c.view.info(`resumed ${id}`)
},
})
}
/** Delete a session after asking. The hub lets go of it first and the web UI deletes the chat
* it is; `current`: the one open here, so a new session takes its place. `then`: what follows,
* asked or not — the Resume picker opens again. */
function confirmDelete(c: CommandCtx, id: string, current = false, then?: () => void) {
const title = c.app.store?.session(id)?.title || id
c.openDialog({
title: `Delete “${title.slice(0, 60)}”?`,
options: [
{ label: "Delete", value: "yes", description: "its conversation goes, here and in the web UI; the files it changed stay" },
{ label: "Cancel", value: "no" },
],
onSelect: async (v) => {
if (v !== "yes") return then?.()
// A deleted session's jobs go with it.
c.app.registry.stopAll(id)
if (current) {
c.app.cancel()
c.app.newSession()
c.view.clear()
}
// Without a share (none in tests), the same way `lembas sessions delete` goes: the
// instance hears of it through the hub, or later from the pending list.
const store = c.app.store
const why = c.share ? await c.share.deleted(id) : store ? await deleteEverywhere(store, id) : undefined
c.view.info(why ? `not deleted: ${why}` : "deleted", why ? "warn" : "info")
then?.()
},
})
}