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.

7.3 KB
Raw Blame
import path from "node:path";
import process from "node:process";
import { createPrinter } from "./mso-agent-print.mjs";

function skillRows(skillsData) {
  return Array.isArray(skillsData?.skills) ? skillsData.skills : [];
}

function inside(root, cwd) {
  if (!root) return false;
  const rel = path.relative(path.resolve(root), path.resolve(cwd));
  return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
}

function sameSkill(a, b) {
  return Boolean(a?.id && b?.id && String(a.id) === String(b.id));
}

export function skillRuntimeState(session, skill) {
  if (skill?.trust === "untrusted") return "blocked";
  if (sameSkill(session?.activeSkill, skill)) return "invoking";
  if (sameSkill(session?.pendingSkill, skill)) return "queued";
  if (sameSkill(session?.lastInvokedSkill, skill)) return "invoked";
  return "ready";
}

function skillStateVisual(state, C) {
  if (state === "blocked") return { marker: "!", label: "blocked", color: C.warn };
  if (state === "queued") return { marker: "◆", label: "queued", color: C.warn };
  if (state === "invoking") return { marker: "◆", label: "invoking", color: C.c };
  if (state === "invoked") return { marker: "✓", label: "invoked", color: C.c };
  return { marker: "◇", label: "ready", color: C.blue };
}

export function beginSkillInvocation(session, skill, C, options = {}) {
  if (!skill) return;
  session.activeSkill = skill;
  const scope = skill.project?.name ? `project ${skill.project.name}` : "global";
  const { print } = createPrinter({ session, colors: C, write: options?.print });
  print(`${C.c}${C.bold}◆ skill /${skill.name} invoking${C.reset}${C.dim} · ${scope} · ${skill.trust || "trusted"}${C.reset}`);
}

export function endSkillInvocation(session, skill) {
  if (skill) session.activeSkill = null;
}

export function currentSkillProject(skillsData, cwd = process.cwd()) {
  const projects = new Map();
  for (const skill of skillRows(skillsData)) {
    const project = skill?.project;
    if (!project?.id || !project?.path || !inside(project.path, cwd)) continue;
    projects.set(project.id, project);
  }
  return [...projects.values()].sort((a, b) => String(b.path).length - String(a.path).length)[0] || null;
}

export function resolveSlashSkill(skillsData, requested, cwd = process.cwd()) {
  const raw = String(requested || "").replace(/^\/+/, "").trim();
  if (!raw) return { skill: null, ambiguous: [] };
  const rows = skillRows(skillsData);

  // Exact catalog ids contain a slash for project skills. /skill <exact-id> is
  // the explicit escape hatch when the same skill name exists in many projects.
  if (raw.includes("/")) {
    const exact = rows.find((skill) => String(skill.id) === raw) || null;
    return { skill: exact, ambiguous: [] };
  }

  const named = rows.filter((skill) => String(skill.name || skill.id) === raw);
  const current = currentSkillProject(skillsData, cwd);
  if (current) {
    const local = named.filter((skill) => skill.project?.id === current.id);
    if (local.length === 1) return { skill: local[0], ambiguous: [], currentProject: current };
    if (local.length > 1) return { skill: null, ambiguous: local, currentProject: current };
  }

  const globals = named.filter((skill) => !skill.project);
  if (globals.length === 1) return { skill: globals[0], ambiguous: [], currentProject: current };
  if (globals.length > 1) return { skill: null, ambiguous: globals, currentProject: current };

  // Never silently import a project skill from some other repository just because
  // its name happens to be globally unique. Require an exact catalog id instead.
  return { skill: null, ambiguous: named, currentProject: current };
}

export function slashSkillNames(skillsData, cwd = process.cwd()) {
  const names = [...new Set(skillRows(skillsData)
    .filter((skill) => skill?.trust !== "untrusted")
    .map((skill) => String(skill.name || "").trim())
    .filter(Boolean))];
  return names.filter((name) => Boolean(resolveSlashSkill(skillsData, name, cwd).skill)).sort();
}

export async function loadSlashSkill(api, sessionId, skill) {
  if (!skill?.id) throw new Error("skill has no catalog id");
  const out = await api("/api/v1/agent-tools", {
    method: "POST",
    body: JSON.stringify({ name: "skills_read", input: { name: skill.id }, sessionId }),
  });
  if (!out?.ok) throw new Error(String(out?.result || `could not read skill ${skill.id}`));
  let data;
  try { data = JSON.parse(String(out.result || "{}")); }
  catch { throw new Error(`skill ${skill.id} returned an invalid read payload`); }
  if (data.instructionsWithheld) {
    throw new Error(`/${skill.name || skill.id} is not executable yet (${data.reason || `trust=${data.trust || skill.trust || "untrusted"}`})`);
  }
  if (!data.content) throw new Error(`skill ${skill.id} has no readable instructions`);
  return {
    id: String(data.id || skill.id),
    name: String(data.name || skill.name || skill.id),
    description: String(data.description || skill.description || ""),
    trust: String(data.trust || skill.trust || ""),
    project: data.project || skill.project || null,
    content: String(data.content),
  };
}


export function printSkillChoices(rows, C, options = {}) {
  if (!rows?.length) return;
  const { print } = createPrinter({ colors: C, session: options?.session, write: options?.print });
  print(`${C.warn}skill name is project-specific or ambiguous; choose one exact id:${C.reset}`);
  for (const skill of rows.slice(0, 20)) {
    const project = skill.project?.name ? ` · ${skill.project.name}` : " · global";
    print(`  /skill ${skill.id}${project}`);
  }
}

export function printSkills(session, C, query = "", cwd = process.cwd(), options = {}) {
  const { print } = createPrinter({ session, colors: C, write: options?.print });
  const all = skillRows(session.state.skills);
  const q = query.trim().toLowerCase();
  const current = currentSkillProject(session.state.skills, cwd);
  const scoped = all.filter((skill) => !skill.project || (current && skill.project?.id === current.id));
  let rows = q
    ? all.filter((skill) => `${skill.name || ""} ${skill.id || ""} ${skill.description || ""}`.toLowerCase().includes(q))
    : scoped;
  rows = rows.slice().sort((a, b) => {
    const ap = current && a.project?.id === current.id ? 0 : a.project ? 2 : 1;
    const bp = current && b.project?.id === current.id ? 0 : b.project ? 2 : 1;
    return ap - bp || String(a.name || a.id).localeCompare(String(b.name || b.id));
  });
  if (current) print(`${C.bold}Current project skills:${C.reset} ${current.name} · ${current.path}`);
  const visible = rows.slice(0, 80);
  for (const skill of visible) {
    const scope = skill.project?.name ? `project:${skill.project.name}` : "global";
    const trust = skill.trust === "untrusted" ? "untrusted" : skill.trust || "";
    const state = skillRuntimeState(session, skill);
    const visual = skillStateVisual(state, C);
    const description = skill.description
      ? ` — ${String(skill.description).replace(/[\r\n\t]+/g, " ").slice(0, 120)}`
      : "";
    print(`  ${visual.color}${visual.marker} /${skill.name}${C.reset}  ${C.dim}${visual.label} · ${scope}${trust ? ` · ${trust}` : ""}${C.reset}${description}`);
  }
  if (rows.length > visible.length) print(`${C.dim}… ${rows.length - visible.length} more; refine with /skills <query>${C.reset}`);
  print(`${C.dim}Use /<skill> [prompt], or /skill <exact-id> [prompt] for an ambiguous project skill.${C.reset}`);
}