Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

10.4 KB
Raw Blame
import process from "node:process";
import { activityText, formatDuration } from "./mso-agent-activity.mjs";
import { cellWidth, stripAnsi } from "./tui/cells.mjs";
import {
  applyTheme,
  C,
  colorEnabled,
  currentThemeName,
  THEME_NAMES,
  themeNames,
  THEMES,
} from "./mso-agent-theme.mjs";

export {
  applyTheme,
  C,
  colorEnabled,
  currentThemeName,
  THEME_NAMES,
  themeNames,
  THEMES,
};

export const MSO_TITLE_ART = [
  "   ████████████",
  "  ██          ██",
  "  ██   ██ ██ ██   ██████████████████████",
  "  ██                                  ██      ███╗   ███╗███████╗ ██████╗ ",
  "  ██                                  ██      ████╗ ████║██╔════╝██╔═══██╗",
  "  ██       ██                ██       ██      ██╔████╔██║███████╗██║   ██║",
  "  ██         ██                ██     ██      ██║╚██╔╝██║╚════██║██║   ██║",
  "  ██       ██    ████████    ██       ██      ██║ ╚═╝ ██║███████║╚██████╔╝",
  "  ██                                  ██      ╚═╝     ╚═╝╚══════╝ ╚═════╝ ",
  "  ██                                  ██",
  "   ████████████████████████████████████",
  "                                                       ── MSO Agent ──",
];

export function titleColors() {
  if (!colorEnabled()) return MSO_TITLE_ART.map(() => "");
  const theme = currentThemeName();
  if (theme === "light") {
    return [
      "\x1b[38;2;76;29;149m", "\x1b[38;2;76;29;149m", "\x1b[38;2;46;44;168m", "\x1b[38;2;46;44;168m",
      "\x1b[38;2;21;94;183m", "\x1b[38;2;21;94;183m", "\x1b[38;2;10;110;168m", "\x1b[38;2;10;110;168m",
      "\x1b[38;2;13;122;112m", "\x1b[38;2;13;122;112m", "\x1b[38;2;13;122;86m", "\x1b[38;2;13;122;86m",
    ];
  }
  if (theme === "mono") return MSO_TITLE_ART.map(() => "");
  return [
    "\x1b[38;2;155;81;224m", "\x1b[38;2;155;81;224m", "\x1b[38;2;112;111;245m", "\x1b[38;2;112;111;245m",
    "\x1b[38;2;69;142;255m", "\x1b[38;2;69;142;255m", "\x1b[38;2;45;184;255m", "\x1b[38;2;45;184;255m",
    "\x1b[38;2;34;211;206m", "\x1b[38;2;34;211;206m", "\x1b[38;2;46;229;157m", "\x1b[38;2;46;229;157m",
  ];
}

// The gradient is decoration, not semantics. It follows the theme so a light
// terminal does not get a near-white banner on white.
export const MSO_TITLE_COLORS = titleColors();

/** Compact per-turn receipt replacing the rows each tool used to claim. */
export function completedReceipt(calls, elapsedMs) {
  const rows = Array.isArray(calls) ? calls : [];
  if (!rows.length) return "";
  const ok = rows.filter((row) => row.ok).length;
  const failed = rows.length - ok;
  return activityText([
    `${rows.length} tool${rows.length === 1 ? "" : "s"}`,
    failed ? `${failed} failed` : "all ok",
    formatDuration(elapsedMs || 0),
  ]);
}

function plain(value) { return stripAnsi(value); }
function width(value) { return cellWidth(value); }
export function fit(value, max) {
  const source = plain(value).replace(/[\r\n\t]+/g, " ");
  if (max <= 0) return "";
  // Measured in CELLS: a CJK character is two columns, so the old code-point count returned a "fitted"
  // string up to twice the budget and the box row wrapped.
  if (cellWidth(source) <= max) return source;
  if (max <= 1) return "…".slice(0, max);
  let out = "", used = 0;
  for (const ch of source) {
    const w = cellWidth(ch);
    if (used + w > Math.max(0, max - 1)) break;
    out += ch;
    used += w;
  }
  return `${out}…`;
}
function pad(value, max) {
  const clean = fit(value, max);
  return clean + " ".repeat(Math.max(0, max - width(clean)));
}
function rule(char, n) { return char.repeat(Math.max(0, n)); }
function terminalWidth() {
  const cols = Number(process.stdout.columns || 104);
  // The old floor of 72 ignored the real width, so on a 60-column terminal every box row was drawn 74
  // cells wide. The floor exists to keep the banner readable, but readability cannot exceed the screen.
  return Math.max(24, Math.min(118, cols - 2));
}
function countSkills(data) { return Array.isArray(data?.skills) ? data.skills.length : 0; }
function summarizeInfra(data) {
  const rows = Array.isArray(data?.providers) ? data.providers : [];
  const ready = rows.filter((row) => row.configured).map((row) => String(row.id));
  return { ready, total: rows.length };
}
function toolCategory(name) {
  if (/^fs_/.test(name)) return "files";
  if (/^(sys_|apps_|browser_)/.test(name)) return "system";
  if (/^(project|projects)_/.test(name)) return "projects";
  if (/^(workflow_|skills_|agent_|a2a_)/.test(name)) return "agent";
  if (/^(dokploy_|cloudflare_|hostinger_|infra_)/.test(name)) return "infra";
  return "other";
}
function groupedToolLines(tools, maxLines = 5) {
  const order = ["files", "system", "projects", "agent", "infra", "other"];
  const groups = new Map(order.map((key) => [key, []]));
  for (const tool of tools) groups.get(toolCategory(String(tool.name)))?.push(String(tool.name));
  return order.filter((key) => groups.get(key)?.length).slice(0, maxLines).map((key) => ({
    label: key, value: groups.get(key).join(", "),
  }));
}
function groupedSkillLines(skills, maxLines = 5) {
  const rows = Array.isArray(skills) ? skills : [];
  const groups = new Map();
  for (const skill of rows) {
    const label = String(skill.category || skill.source || "skills").replace(/^mso[:/ -]?/i, "") || "skills";
    if (!groups.has(label)) groups.set(label, []);
    const name = String(skill.id || skill.name || "");
    if (name) groups.get(label).push(name);
  }
  return [...groups.entries()].slice(0, maxLines).map(([label, names]) => ({ label, value: names.join(", ") }));
}
function kv(label, value, widthLimit) {
  const labelWidth = Math.min(13, Math.max(8, widthLimit > 36 ? 12 : 9));
  return `${pad(label, labelWidth)} ${fit(value, Math.max(0, widthLimit - labelWidth - 1))}`;
}
// Body rows are drawn as "│ " + content + " │", which costs four cells of the two border columns, so the
// content budget is two cells less than the space between the borders. Padding straight to `inner` made
// every body row two cells wider than the box above it.
function panelLine(left, right, leftWidth, rightWidth, gap = 3) {
  return `│ ${pad(left, leftWidth)}${" ".repeat(gap)}${pad(right, rightWidth)} │`;
}
function panelSingle(text, inner) { return `│ ${pad(text, Math.max(0, inner - 2))} │`; }
function panelTitle(title, inner) {
  const label = ` ${title} `;
  const remaining = Math.max(0, inner - width(label));
  const before = Math.floor(remaining / 2);
  return `┌${rule("─", before)}${label}${rule("─", remaining - before)}┐`;
}

export function printAgentBanner(s, agentSession, { base, version, print = (line = "") => console.log(line) }) {
  const totalWidth = terminalWidth();
  const inner = totalWidth - 2;
  const titleArtWidth = Math.max(...MSO_TITLE_ART.map(width));
  if (totalWidth >= titleArtWidth) {
    const colors = titleColors();
    MSO_TITLE_ART.forEach((line, index) => print(`${colors[index]}${C.bold}${line}${C.reset}`));
  } else print(`${C.blue}${C.bold}── MSO Agent ──${C.reset}`);
  print();

  const byScope = { read: 0, write: 0, exec: 0 };
  for (const tool of s.tools) byScope[tool.scope] = (byScope[tool.scope] || 0) + 1;
  const infra = summarizeInfra(s.infra);
  const tools = groupedToolLines(s.tools, 5);
  const skills = groupedSkillLines(s.skills?.skills, 5);
  const model = `${s.config?.provider ?? "—"}/${s.config?.model ?? "—"}`;
  const sessionTitle = fit(agentSession?.title || "MSO Agent session", 52);
  const sessionName = `@${agentSession?.name || "agent"}`;
  const title = fit(`MSO Agent${version ? ` v${version}` : ""} · ${sessionName} · ${sessionTitle}`, inner - 2);

  print(panelTitle(title, inner));
  if (totalWidth >= 94) {
    const gap = 4;
    const leftWidth = Math.max(30, Math.floor((inner - gap) * 0.37));
    const rightWidth = inner - gap - leftWidth;
    const left = [
      `${C.bold}Runtime${C.reset}`, kv("model", model, leftWidth), kv("name", sessionName, leftWidth), kv("session", sessionTitle, leftWidth),
      kv("endpoint", base, leftWidth), "", `${C.bold}Capabilities${C.reset}`,
      fit(`${byScope.read} read · ${byScope.write} write · ${byScope.exec} exec`, leftWidth),
      fit("write/exec approval-gated", leftWidth), "", `${C.bold}Infrastructure${C.reset}`,
      fit(`${infra.ready.length}/${infra.total} ready`, leftWidth),
      // Product-facing setup is /integrations: runCli(["integrations", ...args])
      fit(infra.ready.length ? infra.ready.join(" · ") : "run /integrations to connect", leftWidth),
    ];
    const right = [`${C.bold}Available Tools${C.reset}`];
    for (const row of tools) right.push(kv(row.label, row.value, rightWidth));
    right.push("", `${C.bold}Available Skills${C.reset}`);
    for (const row of skills) right.push(kv(row.label, row.value, rightWidth));
    const rows = Math.max(left.length, right.length);
    for (let i = 0; i < rows; i++) print(panelLine(left[i] || "", right[i] || "", leftWidth, rightWidth, gap));
  } else {
    const lines = [
      `${C.bold}Runtime${C.reset}`, kv("model", model, inner), kv("name", sessionName, inner), kv("session", sessionTitle, inner), kv("endpoint", base, inner), "",
      `${C.bold}Available Tools${C.reset}`, ...tools.map((row) => kv(row.label, row.value, inner)), "",
      `${C.bold}Available Skills${C.reset}`, ...skills.map((row) => kv(row.label, row.value, inner)), "",
      `${C.bold}Capabilities${C.reset}`, `${byScope.read} read · ${byScope.write} write · ${byScope.exec} exec · write/exec approval-gated`,
      `${C.bold}Infrastructure${C.reset}`, `${infra.ready.length}/${infra.total} ready · ${infra.ready.length ? infra.ready.join(" · ") : "run /integrations to connect"}`,
    ];
    for (const line of lines) print(panelSingle(line, inner));
  }
  print(`└${rule("─", inner)}┘`);
  print(fit(`${C.dim}${s.tools.length} tools · ${countSkills(s.skills)} skills · /skills · /<skill> · /help${C.reset}`, inner));
  print();
  print(fit(`${C.bold}Welcome to MSO Agent!${C.reset} Type your message or /help for commands.`, inner));
}