Skip to content

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

3.6 KB
Raw Blame
/** Wire shapes for automations that run when an engine's usage limit resets, and for the engines' limit state
 * the Automations page, the chat and the calendar show. */

export type LimitResetRepeat = "once" | "every_reset";

/** A routine schedule that runs when an engine's usage limit resets. */
export interface LimitResetSchedule {
  type: "limit_reset";
  /** The engine instance id whose limit it waits for, e.g. "claude" or "codex". */
  engine: string;
  repeat: LimitResetRepeat;
  /** Derived by the server, never taken from input: the reset plus a short buffer, or null while the time is not known. */
  at: number | null;
}

/** As a request or an edit sends it: the server derives `at`. */
export type LimitResetScheduleInput = Omit<LimitResetSchedule, "at" | "repeat"> & { repeat?: LimitResetRepeat; at?: number | null };

/** How long after a reset the run starts, so the provider has rolled its window over. */
export const AFTER_RESET_MS = 60_000;

/** From this share of a window used, an engine is shown as near its limit. */
export const NEAR_LIMIT_PERCENT = 90;

export type LimitWindowName = "5-hour" | "weekly" | "other";

/** One usage window as the engine reported it. */
export interface LimitWindowUsage {
  window: LimitWindowName;
  /** 0–100. */
  usedPercent: number;
  /** Epoch ms when it resets; null when not known. */
  resetsAt: number | null;
}

/** One engine's usage limit as OpenMausBot knows it now. */
export interface EngineLimit {
  /** Engine instance id. */
  engine: string;
  /** The engine's display name ("Claude", "Codex", or the instance's own name). */
  name: string;
  driver: "claude" | "codex" | "grok" | "opencode" | "antigravity";
  /** The window that decides the reset: the fullest, or the one refusing work. */
  window: LimitWindowName | null;
  /** 0–100, or null when the engine reported no window. */
  usedPercent: number | null;
  /** Epoch ms when that window resets; null when not known. */
  resetsAt: number | null;
  /** The engine refuses work until resetsAt. */
  blocked: boolean;
  /** Blocked only by a refusal's hold, which "Try again now" can end; absent when a window at its limit blocks it. */
  held?: boolean;
  /** Whose allowance the windows measure when it is not the whole engine's: "Gemini" on Antigravity, whose other models
   * draw on allowances of their own. Such a limit is shown only: it is never `blocked`, its `resetsAt` is that window's
   * reset and not the engine's, and no limit-reset automation waits for it. Absent when the windows are the engine's; a
   * hold on the whole engine is reported without it. */
  meter?: string;
  /** Every window the engine reported, in the engine's order. A server that predates it sends only the deciding window above. */
  windows?: LimitWindowUsage[];
  /** When these windows were read, when that is before the report's checkedAt: the latest read failed and this is the
   * reading before it. Absent when the windows are from the latest read. */
  checkedAt?: number;
}

export interface EngineLimitsReport {
  checkedAt: number;
  engines: EngineLimit[];
}

export function isLimitResetSchedule(schedule: { type: string } | null | undefined): schedule is LimitResetSchedule {
  return schedule?.type === "limit_reset";
}

/** A one-time automation: a fixed time, or the next reset only. */
export function runsOnce(schedule: { type: string; repeat?: unknown }): boolean {
  return schedule.type === "once" || (schedule.type === "limit_reset" && schedule.repeat !== "every_reset");
}

export function nearLimit(limit: Pick<EngineLimit, "usedPercent" | "blocked">): boolean {
  return limit.blocked || (limit.usedPercent ?? 0) >= NEAR_LIMIT_PERCENT;
}