Skip to content

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

4.9 KB
Raw Blame
import { isCountedSkip, type Routine, type RoutineComputer, type RoutineRun } from "./routines";
import { activeLocale, t } from "./i18n";
import { routineLineText } from "./activity-text";
import { clockOptions } from "./clock-time";

export function routineRunTime(run: RoutineRun): number {
  return run.createdAt || run.startedAt || run.scheduledFor;
}

/** The routine's newest run, leaving out a row that only counts skipped occurrences. */
export function latestRoutineRun(routineId: string, runs: readonly RoutineRun[]): RoutineRun | undefined {
  return runs.filter((run) => run.routineId === routineId && !isCountedSkip(run))
    .reduce<RoutineRun | undefined>((latest, run) => !latest || routineRunTime(run) > routineRunTime(latest) ? run : latest, undefined);
}

/** A run that failed only because a step did, after its reply came back: either recorded as failed with the reply in
 * its output, or completed with the failing step kept in its error line. */
export function partlyDone(run: RoutineRun): boolean {
  return !run.goalStatus && run.partial === true &&
    (run.status === "failed" || (run.status === "completed" && !!run.error));
}

export function routineRunLabel(run: RoutineRun): string {
  if (run.goalStatus) return t(`routines.goal.${run.goalStatus}`);
  const status = t(partlyDone(run) ? "routines.status.partial" : `routines.status.${run.status}`);
  return run.retried && run.status !== "queued" ? t(`routines.retried.${run.retried}`, { status }) : status;
}

export function routineRunTone(run: RoutineRun): string {
  if (run.status === "waiting" || partlyDone(run) || ["needs-input", "limit-reached", "paused"].includes(run.goalStatus ?? "")) return "text-warning";
  if (["failed", "missed"].includes(run.status) || ["failed", "blocked"].includes(run.goalStatus ?? "")) return "text-danger";
  if (run.status === "running") return "text-accent";
  if (run.status === "completed" && (!run.goalStatus || run.goalStatus === "completed")) return "text-success";
  return "text-ink-secondary";
}

export function routineDateTime(at: number): string {
  return new Date(at).toLocaleString(activeLocale(), { month: "short", day: "numeric", ...clockOptions(activeLocale()) });
}

export function routineFinished(routine: Routine): boolean {
  return routine.schedule.type === "once" && routine.nextRunAt == null && routine.schedule.at <= Date.now();
}

export function routineScheduleState(routine: Routine): string {
  if (routineFinished(routine)) return t("routines.finishedSchedule");
  return routine.enabled ? t("routines.activeSchedule") : t("routines.pausedSchedule");
}

/** Why OpenMausBot keeps this routine paused, while it does (its owner left: shared/routines.ts RoutineHoldCause). */
export function routineHeldNote(routine: Routine): string | null {
  return routine.heldFor ? t(`routines.heldFor.${routine.heldFor}`) : null;
}

export function routineNextLabel(routine: Routine): string {
  if (!routine.enabled) return routineScheduleState(routine);
  return routine.nextRunAt == null ? t("routines.noNextRun") : t("routines.nextRun", { time: routineDateTime(routine.nextRunAt) });
}

/** The words for a computer a routine keeps for its runs. */
export function routineComputerLabel(computer: RoutineComputer, cloudHome: boolean): string {
  if (computer === "cloud") return t("routines.runsOn.botCloud");
  if (computer === "vm") return t("routines.runsOn.botVm");
  if (computer === "browser") return t("routines.runsOn.browser");
  return t(cloudHome ? "routines.runsOn.cloudHome" : "routines.runsOn.local");
}

/** `computer`: the bot's Works on setting; `kept`: the routine's own computer, which its runs use instead. */
export function routineRunsOn({ roomGoal, runOn, computer, kept, cloudHome }: {
  roomGoal: boolean; runOn?: string; computer?: string | null; kept?: RoutineComputer; cloudHome: boolean;
}): { label: string; hint: string } {
  const hint = t(cloudHome ? "routines.runsOn.cloudHomeHint" : "routines.runsOn.localHint");
  if (roomGoal) return { label: t(cloudHome ? "routines.runsOn.teamCloudHome" : "routines.runsOn.team"), hint };
  if (runOn === "cloud") return { label: t("routines.runsOn.boat"), hint: t(cloudHome ? "routines.runsOn.boatHintCloudHome" : "routines.runsOn.boatHint") };
  if (kept) return { label: routineComputerLabel(kept, cloudHome), hint: t("routines.runsOn.keptHint") };
  if (computer === "cloud") return { label: t("routines.runsOn.botCloud"), hint };
  if (computer === "vm" && !cloudHome) return { label: t("routines.runsOn.botVm"), hint };
  return { label: t(cloudHome ? "routines.runsOn.cloudHome" : "routines.runsOn.local"), hint };
}

export function routineRunText(run: RoutineRun, field: "output" | "error" | "attention"): string {
  const english = run[field] ?? "";
  if (field === "output") return routineLineText(run.outputCode, english);
  return field === "error" ? routineLineText(run.errorCode, english, run.errorParams) : routineLineText(run.attentionCode, english, run.attentionParams);
}