Skip to content

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

12.6 KB
Raw Blame
/** Routine wire shapes: the routine and run records as they ride the REST
 * snapshot and the `routine` / `routine.run` live frames. */
import type { LimitResetSchedule, LimitResetScheduleInput } from "./limit-reset.ts";
import type { RoutineCronSchedule } from "./routine-schedule.ts";
import type { ModelSelection } from "./wire.ts";
import type { ServerTextParams } from "./server-text.ts";

export interface RoutineIntervalWindow {
  start: string;
  end: string;
}

export type RoutineSchedule =
  | RoutineCronSchedule
  | LimitResetSchedule
  | { type: "once"; at: number }
  | { type: "daily"; time: string; weekdays: number[] }
  | {
    type: "interval";
    everyMinutes: number;
    anchorAt: number;
    /** Local weekdays (Sunday = 0). Missing means every day. */
    weekdays?: number[];
    /** Local wall-clock window. Missing means all day. */
    window?: RoutineIntervalWindow;
    /** Inclusive epoch-millisecond cutoff. Missing means never. */
    endsAt?: number;
  };

export type RoutineScheduleInput =
  | Extract<RoutineSchedule, { type: "once" | "daily" | "cron" }>
  | LimitResetScheduleInput
  | {
    type: "interval";
    everyMinutes: number;
    anchorAt: number;
    /** `null` explicitly restores the every-day default on updates. */
    weekdays?: number[] | null;
    /** `null` explicitly restores the all-day default on updates. */
    window?: RoutineIntervalWindow | null;
    /** `null` explicitly removes an existing end date on updates. */
    endsAt?: number | null;
  };

export type RoutineRunOn = "maus" | "cloud";

export type RoutineTarget = "bot" | "room-goal";
export type RoutineGoalStatus =
  | "completed"
  | "needs-input"
  | "blocked"
  | "limit-reached"
  | "paused"
  | "stopped"
  | "failed";

export interface RoutineContextAttachment {
  id: string;
  kind: "file" | "image";
  name: string;
  path: string;
  size: number;
}

/** "api": an outside caller through the bot's API; "webhook": an outside event. Both run as nobody's work. */
export type RoutineRunTrigger = "schedule" | "manual" | "webhook" | "startup" | "api";

/** A check a routine runs on each tick before any AI turn, in plain code: `calendar` reads the next 36 hours, `mail`
 * new inbox mail since the last check, `runs` other routines' finished runs. Nothing new means no AI turn. */
export type RoutineCheck = "calendar" | "mail" | "runs";
export const ROUTINE_CHECKS: readonly RoutineCheck[] = ["calendar", "mail", "runs"];

/** What a routine's runs may do. `read-and-draft`: read Gmail and Google Calendar and write reply drafts; every other
 * tool is refused outright, never turned into a card. Missing means the bot's own level. */
export type RoutineAccess = "read-and-draft";

/** Where a routine's results go. `brief`: they become rows of the main agent's brief, not chat lines of their own. */
export type RoutineDelivery = "brief";

/** Why OpenMausBot paused a routine by itself, after its owner left: no AI of theirs is signed in here any more, they
 * removed every connected app and server, or the plan was cancelled. It turns back on once that changes. */
export type RoutineHoldCause = "plan_cancelled" | "no_engine" | "no_connections";
export const ROUTINE_HOLD_CAUSES: readonly RoutineHoldCause[] = ["plan_cancelled", "no_engine", "no_connections"];

/** What a finished run left in the chat it reports to: nothing, because it
 * found nothing to tell; a report waiting for a turn there to end; or the bot
 * message it wrote. */
export type RoutineRunReport =
  | { state: "quiet" }
  | { state: "pending" }
  | { state: "posted"; threadId: string; messageId: string };

export type RoutineRunStatus =
  | "queued"
  | "running"
  | "waiting"
  | "completed"
  | "failed"
  | "cancelled"
  | "missed";

/** Why a run started a second time by itself: its engine stalled, OpenMausBot
 * restarted while it ran, the provider rate-limited it or failed for a moment,
 * it waited for its engine's usage limit to reset, or it failed some other way.
 * A run gets one. */
export type RoutineRetryCause = "stalled" | "restarted" | "rate_limited" | "provider" | "usage_limit" | "failed";

/** The statuses both failure indicators count: a run that went wrong and has
 * not been acknowledged. */
export const ROUTINE_PROBLEM_STATUSES: readonly RoutineRunStatus[] = ["failed", "missed"];

/** The limits the server applies to the runs that set none of their own: the minutes of work a bot's run may do,
 * and the same for a room goal's run, which stays running across every member's turn. */
export interface RoutineRunLimits {
  runMinutes: number;
  goalMinutes: number;
}

/** Minutes of work a run may do when its routine sets no limit of its own: a bot's run, and a room goal's run, which
 * stays running across every member's turn. */
export const ROUTINE_RUN_LIMIT_MINUTES = 180;
export const ROUTINE_GOAL_RUN_LIMIT_MINUTES = 720;
/** The longest limit a routine may set for itself (`timeoutMinutes`), and the shortest. */
export const ROUTINE_RUN_LIMIT_MAX_MINUTES = 1_440;
export const ROUTINE_RUN_LIMIT_MIN_MINUTES = 5;

/** A run's `attention` while it waits only on work it handed to teammates: nothing is asked of the person. */
export const ROUTINE_HANDOFF_WAIT = "Waiting for work it handed on to finish";

export function isRoutineProblemRun(run: { status: RoutineRunStatus }): boolean {
  return ROUTINE_PROBLEM_STATUSES.includes(run.status);
}

/** A missed row that only counts occurrences skipped while the routine was otherwise at work, or while its tries are
 * spaced out, and that the owner was not told about: it belongs in the history, and is never the routine's latest result. */
export function isCountedSkip(run: Pick<RoutineRun, "skipped">): boolean {
  return run.skipped !== undefined && run.skipped.told !== true;
}

/** The logs view's status filter: every run, the composite problem set the
 * indicators count, or one exact status. */
export type RoutineRunStatusFilter = "all" | "problems" | RoutineRunStatus;

/** A computer a routine's runs can be kept on: the person's own, the Local VM, the cloud computer, or the built-in browser only. */
export type RoutineComputer = "local" | "vm" | "cloud" | "browser";
export const ROUTINE_COMPUTERS: readonly RoutineComputer[] = ["local", "vm", "cloud", "browser"];

export interface Routine {
  id: string;
  name: string;
  prompt: string;
  target: RoutineTarget;
  botId: string;
  /** The room a "room-goal" runs in; none runs the goal in the bot's own one-on-one chat, a room of one. */
  groupId?: string;
  runOn: RoutineRunOn;
  enabled: boolean;
  schedule: RoutineSchedule;
  durationMinutes: number;
  /** How many minutes a run may take before it is stopped, 5 to 1,440. Missing means the default limit on its work:
   * ROUTINE_RUN_LIMIT_MINUTES, or ROUTINE_GOAL_RUN_LIMIT_MINUTES for a room goal. */
  timeoutMinutes?: number;
  overlap?: "skip" | "queue";
  /** Each run starts with the previous run's report. */
  continuity?: boolean;
  /** The model each run's thread uses instead of its bot's. Missing means the bot's model. */
  modelSelection?: ModelSelection;
  /** Also run once each time OpenMausBot starts. */
  runOnStartup?: boolean;
  checks?: RoutineCheck[];
  access?: RoutineAccess;
  deliver?: RoutineDelivery;
  /** When its checks last read everything and found nothing for the AI. */
  checkedAt?: number;
  /** A plain sentence while its checks cannot read an app, so a failed read never looks like nothing new. */
  checkTrouble?: string;
  /** Catalog key for `checkTrouble` when it is one of the server's fixed lines; `checkTrouble` keeps the English. */
  checkTroubleCode?: string;
  /** The next time its checks have something to do on time (a reminder, meeting prep): only this may keep the
   * computer awake or wake a sleeping Cloud. */
  wakeAt?: number;
  skippedRuns?: number;
  lastSkippedAt?: number;
  failureStreak?: number;
  /** Its newest runs in a row whose cloud computer did not start, once there are enough that its scheduled runs are
   * spaced out for it. It stays on. */
  noStartStreak?: number;
  /** Runs that finished in the last seven days, and how many of them completed. */
  weekRuns?: { ok: number; total: number };
  /** A plain sentence when the routine names a path that cannot exist on the computer that runs it. */
  warning?: string;
  /** Set while OpenMausBot keeps it paused because its owner left; turning it on or off clears it. */
  heldFor?: RoutineHoldCause;
  attachments?: RoutineContextAttachment[];
  sourceThreadId?: string;
  /** The computer each run works on, kept from the chat the routine was made in when that chat was set to one.
   * Missing means the bot's own Works on setting. */
  computer?: RoutineComputer;
  resultsThreadId?: string;
  nextRunAt: number | null;
  createdAt: number;
  updatedAt: number;
}

export interface RoutineRun {
  id: string;
  routineId: string;
  routineName: string;
  prompt?: string;
  durationMinutes?: number;
  timeoutMinutes?: number;
  attachments?: RoutineContextAttachment[];
  target: RoutineTarget;
  goalStatus?: RoutineGoalStatus;
  botId: string;
  groupId?: string;
  runOn: RoutineRunOn;
  scheduledFor: number;
  status: RoutineRunStatus;
  manual: boolean;
  triggerSource?: RoutineRunTrigger;
  /** "app": a connected-app event started it; "wait": a wait's follow-up did. triggerSource is then "webhook" (an outside event). */
  eventSource?: "app" | "wait";
  webhookId?: string;
  deliveryId?: string;
  /** Room task created for a team-goal run. */
  executionThreadId?: string;
  sourceThreadId?: string;
  /** The routine's own computer when the run started (Routine.computer). */
  computer?: RoutineComputer;
  resultsThreadId?: string;
  threadId?: string;
  startedAt?: number;
  finishedAt?: number;
  output?: string;
  /** Catalog key for `output` when it is one of the server's fixed lines; `output` keeps the English. */
  outputCode?: string;
  /** Catalog key and params for `error` and for `attention` when the server wrote them from a fixed line. A client words
   * one only while its field still reads as that line's English; the field always keeps the English. */
  errorCode?: string;
  errorParams?: ServerTextParams;
  attentionCode?: string;
  attentionParams?: ServerTextParams;
  error?: string;
  /** A failed run whose reply still came back: only a step it ran failed or wasn't allowed. */
  partial?: boolean;
  /** Set when the run used its one automatic second start, and why. */
  retried?: RoutineRetryCause;
  /** A failed run that only the owner's AI account could have let through (signed out, out of credit, a usage limit, an
   * engine to set up): it spent nothing, so it does not count towards spacing its routine out. */
  ownerCause?: "signed_out" | "credits" | "usage_limit" | "setup";
  /** Concise, redacted question or approval reason while status is waiting,
   * or why a queued run starts later. */
  attention?: string;
  cost?: number | null;
  denials?: string[];
  report?: RoutineRunReport;
  access?: RoutineAccess;
  deliver?: RoutineDelivery;
  /** The items its routine's checks handed this run, so they are kept or released once it ends. */
  checkKey?: string;
  /** On a missed row that stands for occurrences skipped for one cause: how many, the first and last scheduled time,
   * and whether the owner was told. Its `error` says why. */
  skipped?: { count: number; firstAt: number; lastAt: number; told?: true };
  createdAt: number;
  seenAt?: number;
}

export interface RoutineInput {
  name: string;
  prompt: string;
  target?: RoutineTarget;
  botId: string;
  groupId?: string | null;
  runOn?: RoutineRunOn;
  enabled?: boolean;
  schedule: RoutineScheduleInput;
  durationMinutes?: number;
  /** `null` explicitly removes the limit; omission preserves it on updates. */
  timeoutMinutes?: number | null;
  overlap?: "skip" | "queue";
  /** Each run starts with the previous run's report. */
  continuity?: boolean;
  /** `null` removes it on updates; omission preserves it. */
  modelSelection?: ModelSelection | null;
  runOnStartup?: boolean;
  /** `null` explicitly removes them on updates; omission preserves them. */
  checks?: RoutineCheck[] | null;
  access?: RoutineAccess | null;
  deliver?: RoutineDelivery | null;
  attachments?: RoutineContextAttachment[];
  /** Omission preserves routing; null resets it to the bot's main thread (on a Team Cloud, a results chat everyone who
   * sees the bot reads); RESULTS_JUST_ME, on a Team Cloud only, the person's own private chat with the bot. */
  resultsThreadId?: string | null;
  /** Omission preserves it on updates; null clears it, so runs follow the bot's Works on setting again. */
  computer?: RoutineComputer | null;
}

/** On a Team Cloud, "Just me": the routine reports into the person's own private chat with the bot, theirs alone. */
export const RESULTS_JUST_ME = "just-me";