Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.5 KB
Raw Blame
import { execFile } from 'node:child_process';
import { readFile, statfs } from 'node:fs/promises';
import os from 'node:os';

export interface CpuTimes {
  idle: number;
  total: number;
}

export interface DiskUsage {
  totalBytes: number;
  usedBytes: number;
}

export interface MemoryUsage {
  totalBytes: number;
  usedBytes: number;
}

/** Summed across cores, so two readings give the busy share of the whole machine. */
export const readCpuTimes = (): CpuTimes => {
  let idle = 0;
  let total = 0;
  for (const cpu of os.cpus()) {
    const { idle: cpuIdle, irq, nice, sys, user } = cpu.times;
    idle += cpuIdle;
    total += cpuIdle + irq + nice + sys + user;
  }
  return { idle, total };
};

/** Busy share of all cores between two readings, 0–100; null when no time elapsed. */
export const cpuPercentBetween = (prev: CpuTimes, next: CpuTimes): number | null => {
  const total = next.total - prev.total;
  if (total <= 0) return null;
  const busy = total - (next.idle - prev.idle);
  return Math.min(100, Math.max(0, (busy / total) * 100));
};

/**
 * `vm_stat` output β†’ bytes in use the way Activity Monitor counts "Memory
 * Used" (app + wired + compressed). `os.freemem()` on macOS reports only
 * truly free pages, so file cache would read as used and every Mac would
 * look nearly full.
 */
export const parseVmStat = (output: string, totalBytes: number): number | undefined => {
  const pageSize = Number(/page size of (\d+) bytes/.exec(output)?.[1]);
  if (!pageSize) return undefined;
  const pages = (label: string) => {
    const match = new RegExp(`^${label}:\\s+(\\d+)`, 'm').exec(output);
    return match ? Number(match[1]) : 0;
  };
  const used =
    (pages('Anonymous pages') -
      pages('Pages purgeable') +
      pages('Pages wired down') +
      pages('Pages occupied by compressor')) *
    pageSize;
  return Math.min(totalBytes, Math.max(0, used));
};

/** `/proc/meminfo` β†’ total minus MemAvailable (what `free` calls used + non-reclaimable). */
export const parseMemInfo = (content: string): MemoryUsage | undefined => {
  const kb = (label: string) => {
    const match = new RegExp(`^${label}:\\s+(\\d+) kB`, 'm').exec(content);
    return match ? Number(match[1]) * 1024 : undefined;
  };
  const total = kb('MemTotal');
  const available = kb('MemAvailable');
  if (total === undefined || available === undefined) return undefined;
  return { totalBytes: total, usedBytes: Math.max(0, total - available) };
};

const runVmStat = () =>
  new Promise<string>((resolve, reject) => {
    execFile('vm_stat', { timeout: 5000 }, (error, stdout) =>
      error ? reject(error) : resolve(stdout),
    );
  });

export const readMemoryUsage = async (): Promise<MemoryUsage> => {
  const totalBytes = os.totalmem();
  const fallback = { totalBytes, usedBytes: Math.max(0, totalBytes - os.freemem()) };
  try {
    if (process.platform === 'darwin') {
      const used = parseVmStat(await runVmStat(), totalBytes);
      return used === undefined ? fallback : { totalBytes, usedBytes: used };
    }
    if (process.platform === 'linux') {
      return parseMemInfo(await readFile('/proc/meminfo', 'utf8')) ?? fallback;
    }
  } catch {
    // Fall through to the portable reading.
  }
  return fallback;
};

/** 1 / 5 / 15 minute load averages; Windows has none (Node reports zeros). */
export const readLoadAverage = (): [number, number, number] | null => {
  if (process.platform === 'win32') return null;
  const [load1, load5, load15] = os.loadavg();
  return [load1, load5, load15];
};

/**
 * `statfs` reading β†’ the volume's size and the bytes no longer available to
 * the user (total minus `bavail`). Counting against `bavail` rather than
 * `bfree` means a volume reads 100% exactly when the user can no longer write
 * to it (root-reserved blocks included), and on APFS it covers every volume
 * sharing the container, as Finder's storage bar does.
 */
export const diskUsageFromStatfs = (stats: {
  bavail: number;
  blocks: number;
  bsize: number;
}): DiskUsage | undefined => {
  const totalBytes = stats.blocks * stats.bsize;
  if (!(totalBytes > 0)) return undefined;
  const usedBytes = Math.min(totalBytes, Math.max(0, (stats.blocks - stats.bavail) * stats.bsize));
  return { totalBytes, usedBytes };
};

/** The volume holding the home directory β€” where agents' working directories usually live. */
export const readDiskUsage = async (dir = os.homedir()): Promise<DiskUsage | undefined> => {
  try {
    return diskUsageFromStatfs(await statfs(dir));
  } catch {
    return undefined;
  }
};