Skip to content

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

1.9 KB
Raw Blame
import { botShowsUnread } from "./bot-unread";
import { t } from "./i18n";

export type SidebarAttentionBot = {
  unread?: boolean;
  busy?: boolean;
  activity?: "working" | "waiting-on-you" | "idle" | "no-signal" | "dead" | "parked.computer";
  tasks?: Parameters<typeof botShowsUnread>[0]["tasks"];
};

export type SidebarAttentionGroup = {
  unread?: boolean;
  busyBotId?: string | null;
  tasks?: Array<{ busyBotId?: string | null }>;
};

export type SidebarSectionAttention = {
  unread: number;
  waiting: number;
  working: number;
  /** Rows that are unread or wait on the person, each counted once (a waiting bot is often unread too). */
  needsYou?: number;
};

export function sidebarSectionAttention(
  bots: SidebarAttentionBot[],
  groups: SidebarAttentionGroup[],
): SidebarSectionAttention {
  return {
    unread:
      bots.filter(botShowsUnread).length +
      groups.filter((group) => Boolean(group.unread)).length,
    waiting: bots.filter((bot) => bot.activity === "waiting-on-you").length,
    needsYou: bots.filter((bot) => botShowsUnread(bot) || bot.activity === "waiting-on-you").length +
      groups.filter((group) => Boolean(group.unread)).length,
    working:
      bots.filter(
        (bot) =>
          bot.activity === "working" ||
          (Boolean(bot.busy) && bot.activity !== "waiting-on-you"),
      ).length + groups.filter((group) => Boolean(group.busyBotId) || Boolean(group.tasks?.some((task) => task.busyBotId))).length,
  };
}

export function sidebarAttentionLabel(attention: SidebarSectionAttention): string {
  const parts: string[] = [];
  if (attention.waiting > 0) {
    parts.push(t("sidebar.attention.waiting", { count: attention.waiting }));
  }
  if (attention.unread > 0) {
    parts.push(t("sidebar.attention.unread", { count: attention.unread }));
  }
  if (attention.working > 0) {
    parts.push(t("sidebar.attention.working", { count: attention.working }));
  }
  return parts.join(", ");
}