Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

13.0 KB
Raw Blame
import { api } from "@/state/store";
import { activeLocale, t } from "@/lib/i18n";
import { namesLocale } from "@/lib/clock-time";
import { en, type LocaleKey } from "@/locales";
import { toolStepLabel } from "@/lib/tool-step-label";
import { formatUsd, hasFiniteCost, usdText } from "@/lib/usage";
import type {
  TraceDetail, TraceGroup, TraceList, TraceNode, TraceRootSummary, TraceStep, TraceTokens, TraceTool, TraceTurn,
} from "../../shared/usage-trace.ts";

export type UsagePeriod = "today" | "week" | "month" | "lastMonth" | "days30" | "days90";
export const USAGE_PERIODS: readonly UsagePeriod[] = ["today", "week", "month", "lastMonth", "days30", "days90"];

const DAY_MS = 24 * 60 * 60_000;
const day = (date: Date) => date.toISOString().slice(0, 10);

export function usagePeriodRange(period: UsagePeriod, now = new Date()): { from: string; to: string } {
  const year = now.getUTCFullYear();
  const month = now.getUTCMonth();
  if (period === "today") return { from: day(now), to: day(now) };
  if (period === "week") return { from: day(new Date(now.getTime() - 6 * DAY_MS)), to: day(now) };
  if (period === "month") return { from: day(new Date(Date.UTC(year, month, 1))), to: day(now) };
  if (period === "lastMonth") return { from: day(new Date(Date.UTC(year, month - 1, 1))), to: day(new Date(Date.UTC(year, month, 0))) };
  if (period === "days90") return { from: day(new Date(now.getTime() - 89 * DAY_MS)), to: day(now) };
  return { from: day(new Date(now.getTime() - 29 * DAY_MS)), to: day(now) };
}

export function localPeriodRange(period: UsagePeriod, now = new Date()): { from: string; to: string } {
  const shifted = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 12));
  return usagePeriodRange(period, shifted);
}

/** A cost as the trace prints it: a sliver of a cent reads "<$0.001", never "$0.000". */
export function formatCost(usd: number | null | undefined): string {
  if (!hasFiniteCost(usd)) return "—";
  return usd > 0 && usd < 0.001 ? `<${usdText(0.001, 3)}` : formatUsd(usd);
}

export function usageExportHref(range: { from: string; to: string }): string {
  return `/api/usage.csv?from=${encodeURIComponent(range.from)}&to=${encodeURIComponent(range.to)}`;
}

export function fetchTraces(range: { from: string; to: string }, offset = 0): Promise<TraceList> {
  return api(`/api/usage/traces?from=${encodeURIComponent(range.from)}&to=${encodeURIComponent(range.to)}&offset=${offset}&limit=50`);
}

export function fetchTrace(id: string, at?: number): Promise<TraceDetail> {
  return api(`/api/usage/traces/${encodeURIComponent(id)}${at ? `?at=${Math.round(at)}` : ""}`);
}

type TimeUnit = "millisecond" | "second" | "minute" | "hour";
const unitFormats = new Map<string, Intl.NumberFormat>();
const unit = (value: number, name: TimeUnit, digits = 0): string => {
  const id = `${namesLocale()} ${name} ${digits}`;
  if (!unitFormats.has(id)) unitFormats.set(id, new Intl.NumberFormat(namesLocale(), { style: "unit", unit: name, unitDisplay: "narrow", maximumFractionDigits: digits, useGrouping: false }));
  return unitFormats.get(id)!.format(value).replaceAll(" ", "\u00a0");
};
const pair = (big: number, bigUnit: TimeUnit, small: number, smallUnit: TimeUnit) =>
  small ? `${unit(big, bigUnit)}${activeLocale() === "en" ? " " : "\u00a0"}${unit(small, smallUnit)}` : unit(big, bigUnit);

/** "450ms", "6.9s", "51s", "1m 40s", "2h 5m": rounded before the unit is picked. */
export function formatDuration(ms: number): string {
  if (!Number.isFinite(ms) || ms < 0) return "";
  if (ms < 1_000) return unit(Math.round(ms), "millisecond");
  const tenths = Math.round(ms / 100);
  if (tenths < 100) return unit(tenths / 10, "second", 1);
  const seconds = Math.round(ms / 1_000);
  if (seconds < 60) return unit(seconds, "second");
  const minutes = Math.floor(seconds / 60);
  if (minutes < 60) return pair(minutes, "minute", seconds % 60, "second");
  const hours = Math.floor(minutes / 60);
  return pair(hours, "hour", minutes % 60, "minute");
}

const STEPS_MS = [1_000, 2_000, 5_000, 10_000, 20_000, 30_000, 60_000, 120_000, 300_000, 600_000, 1_200_000, 1_800_000, 3_600_000, 7_200_000, 14_400_000];

/** A time axis that ends on a round tick past `spanMs`, with three to five ticks. */
export function timeAxis(spanMs: number): { max: number; ticks: number[] } {
  const span = Math.max(spanMs, 1_000);
  const step = STEPS_MS.find((candidate) => span / candidate <= 4) ?? Math.ceil(span / 4 / 3_600_000) * 3_600_000;
  const max = Math.max(step, Math.ceil((span * 1.08) / step) * step);
  const ticks: number[] = [];
  for (let tick = 0; tick <= max; tick += step) ticks.push(tick);
  return { max, ticks };
}

export function formatTick(ms: number): string {
  if (ms < 60_000) return unit(Math.round(ms / 1_000), "second");
  if (ms < 3_600_000) return pair(Math.floor(ms / 60_000), "minute", Math.round((ms % 60_000) / 1_000), "second");
  return pair(Math.floor(ms / 3_600_000), "hour", Math.round((ms % 3_600_000) / 60_000), "minute");
}

/** Where a node sits on the axis: a turn spans everything under it. */
export function extentOf(node: TraceNode, now: number): { start: number; end: number } {
  const own = { start: node.startedAt, end: node.running ? now : node.endedAt ?? node.startedAt };
  if (node.kind !== "turn" && node.kind !== "group") return own;
  for (const child of node.children) {
    const inner = extentOf(child, now);
    own.start = Math.min(own.start, inner.start);
    own.end = Math.max(own.end, inner.end);
  }
  return own;
}

/** A turn's cost includes every turn under it; a group or a tool has none of its own. */
export function costOf(node: TraceNode): number | null {
  if (node.kind === "step") return node.costUsd ?? null;
  if (node.kind !== "turn") return null;
  let total = node.costUsd;
  for (const child of node.children) {
    if (child.kind !== "turn") continue;
    const inner = costOf(child);
    if (inner !== null) total = (total ?? 0) + inner;
  }
  return total;
}

export function tokensOf(node: TraceNode): TraceTokens | null {
  if (node.kind === "turn") return node.tokens;
  if (node.kind === "step") return node.tokens ?? null;
  return null;
}

const ACTION_PREFIX = /^(?:mcp__.+?__|[a-z][a-z0-9_-]{0,31}__)?(?:agent_browser_|browser_|computer_)?/i;
const sentence = (words: string) => words ? words[0]!.toLocaleUpperCase() + words.slice(1) : words;

const ACTIONS: Record<string, LocaleKey> = {
  "open": "trace.action.open",
  "click": "trace.action.click",
  "double click": "trace.action.doubleClick",
  "right click": "trace.action.rightClick",
  "snapshot": "trace.action.snapshot",
  "eval": "trace.action.eval",
  "status": "trace.action.status",
  "fill": "trace.action.fill",
  "tabs": "trace.action.tabs",
  "get url": "trace.action.getUrl",
  "open url": "trace.action.openUrl",
  "read": "trace.action.read",
  "close": "trace.action.close",
  "screenshot": "trace.action.screenshot",
  "navigate": "trace.action.navigate",
  "wait": "trace.action.wait",
  "wait for": "trace.action.waitFor",
  "wait for text": "trace.action.waitForText",
  "wait for load": "trace.action.waitForLoad",
  "type": "trace.action.type",
  "type text": "trace.action.typeText",
  "scroll": "trace.action.scroll",
  "press": "trace.action.press",
  "key": "trace.action.key",
  "key press": "trace.action.keyPress",
  "get text": "trace.action.getText",
  "get title": "trace.action.getTitle",
  "check": "trace.action.check",
  "uncheck": "trace.action.uncheck",
  "select": "trace.action.select",
  "select option": "trace.action.selectOption",
  "dialog": "trace.action.dialog",
  "upload": "trace.action.upload",
  "tab new": "trace.action.tabNew",
  "tab list": "trace.action.tabList",
  "tab switch": "trace.action.tabSwitch",
  "tab close": "trace.action.tabClose",
  "hover": "trace.action.hover",
  "back": "trace.action.back",
  "forward": "trace.action.forward",
  "reload": "trace.action.reload",
  "drag": "trace.action.drag",
  "move": "trace.action.move",
  "tap": "trace.action.tap",
  "swipe": "trace.action.swipe",
};

/** What a tool did, in words: a browser or computer step names its action, other tools their kind. */
export function toolLabel(tool: Pick<TraceTool, "name" | "icon">): string {
  if (tool.icon === "browser" || tool.icon === "computer" || tool.icon === "phone") {
    const action = tool.name.replace(ACTION_PREFIX, "").replace(/[_-]+/g, " ").trim().toLowerCase();
    if (Object.hasOwn(ACTIONS, action)) return t(ACTIONS[action]!);
    if (action) return sentence(action);
  }
  const step = toolStepLabel({ name: tool.name, summary: undefined });
  const agents = /^mcp__agents__([a-z0-9_]+)$/.exec(tool.name)?.[1];
  if (!agents || step !== sentence(agents.replace(/_+/g, " "))) return step;
  const key = `activity.tool.team.${agents.replace(/_([a-z0-9])/g, (_, letter: string) => letter.toUpperCase())}`;
  return Object.hasOwn(en, key) ? t(key as LocaleKey) : step;
}

export function groupLabel(group: Pick<TraceGroup, "icon" | "count" | "name" | "app" | "server">, appName?: string): string {
  const count = String(group.count);
  switch (group.icon) {
    case "browser": return t("trace.group.browser", { count });
    case "computer": return t("trace.group.computer", { count });
    case "phone": return t("trace.group.phone", { count });
    case "terminal": return t("trace.group.terminal", { count });
    case "files": return t("trace.group.files", { count });
    case "search": return t("trace.group.search", { count });
    case "web": return t("trace.group.web", { count });
    case "app": return t("trace.group.app", { app: appName ?? group.app ?? toolStepLabel({ name: group.name, summary: undefined }), count });
    case "mcp": return t("trace.group.app", { app: group.server ?? group.name, count });
    default: return t("trace.group.other", { tool: toolStepLabel({ name: group.name, summary: undefined }), count });
  }
}

export interface TraceRow {
  node: TraceNode;
  depth: number;
  expandable: boolean;
  open: boolean;
  start: number;
  end: number;
  durationMs: number;
  costUsd: number | null;
}

/** Turns start open and groups closed; `toggled` flips either. */
export function traceRows(turns: readonly TraceTurn[], toggled: ReadonlySet<string>, now: number): TraceRow[] {
  const rows: TraceRow[] = [];
  const visit = (node: TraceNode, depth: number) => {
    const extent = extentOf(node, now);
    const expandable = (node.kind === "turn" || node.kind === "group") && node.children.length > 0;
    const open = expandable && (node.kind === "turn" ? !toggled.has(node.id) : toggled.has(node.id));
    rows.push({ node, depth, expandable, open, start: extent.start, end: extent.end, durationMs: extent.end - extent.start, costUsd: costOf(node) });
    if (open && (node.kind === "turn" || node.kind === "group")) for (const child of node.children) visit(child, depth + 1);
  };
  for (const turn of turns) visit(turn, 0);
  return rows;
}

/** The slowest and the most expensive span below the root, once there are enough spans to compare. */
export function traceBadges(turns: readonly TraceTurn[], now: number): { slowest?: string; costliest?: string } {
  const all: Array<{ node: TraceNode; ms: number; cost: number | null }> = [];
  const visit = (node: TraceNode, depth: number) => {
    if (depth > 0 && (node.kind !== "turn" || node.timed)) {
      const extent = extentOf(node, now);
      all.push({ node, ms: extent.end - extent.start, cost: node.kind === "step" ? node.costUsd ?? null : null });
    }
    if (node.kind === "turn" || node.kind === "group") for (const child of node.children) visit(child, depth + 1);
  };
  for (const turn of turns) visit(turn, 0);
  if (all.length < 3) return {};
  const leaves = all.filter((entry) => entry.node.kind === "step" || entry.node.kind === "tool");
  const slowest = leaves.reduce<(typeof all)[number] | undefined>((best, entry) => entry.ms > (best?.ms ?? 0) ? entry : best, undefined);
  const costliest = all.reduce<(typeof all)[number] | undefined>((best, entry) => (entry.cost ?? 0) > (best?.cost ?? 0) ? entry : best, undefined);
  return { ...(slowest ? { slowest: slowest.node.id } : {}), ...(costliest ? { costliest: costliest.node.id } : {}) };
}

export function threadsOf(turns: readonly TraceTurn[]): Set<string> {
  const threads = new Set<string>();
  const visit = (node: TraceNode) => {
    if (node.kind === "turn") {
      threads.add(node.threadId);
      node.children.forEach(visit);
    }
  };
  turns.forEach(visit);
  return threads;
}

export function isStep(node: TraceNode): node is TraceStep {
  return node.kind === "step";
}

type NamedBot = { id: string; name: string };

/** Usage rows keep the name a bot had when they were booked; a bot that still exists shows under its current name. */
export function currentBotName(bots: readonly NamedBot[], id: string, booked: string): string {
  return bots.find((bot) => bot.id === id)?.name ?? booked;
}

/** A run's title: its own, or "A message to <first bot>" under that bot's current name. */
export function runTitle(root: TraceRootSummary, bots: readonly NamedBot[]): string {
  const first = root.bots[0];
  return root.title || t(`trace.source.${root.source}`, { bot: first ? currentBotName(bots, first.id, first.name) : "" });
}