Skip to content

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

16.1 KB
Raw Blame
import { useEffect, useRef, useState, type CSSProperties, type PointerEvent as ReactPointerEvent, type SyntheticEvent } from "react";
import { createPortal } from "react-dom";

import { cn } from "@/lib/cn";
import { useComposerLayout } from "@/lib/composer-layout";
import { useStoreDispatch } from "@/state/store-select";
import { t } from "@/lib/i18n";
import {
  limitLine, limitsCheckedAt, loadLimits, releaseLimit, reportsUsage, resetTime, usageBars, usageRows, usageTone, useEngineLimits, USAGE_BARS, windowLabel,
  type EngineLimit, type UsageTone,
} from "@/lib/limit-resets";
import type { Routine } from "@/lib/routines";
import { api, type Message } from "@/state/store";
import { failedTurnNotice } from "@/lib/failed-turn";
import { codexUsageLimitReached } from "../../shared/codex-usage-limit";
import { openBotSettings } from "./bot-panel/panel-link";
import { threadUpdatedLabel } from "./SidebarThreadRow";

/** The stopped turn's "Continue at 3:40 PM when your limit resets": one click sets up a one-time automation in this
 * conversation that picks up where it stopped. Undefined when there is nothing to offer. */
export function useLimitResetContinue(botId: string | undefined, threadId: string | undefined, resetsAt: number | undefined) {
  const dispatch = useStoreDispatch();
  const [state, setState] = useState<"idle" | "working" | "set">("idle");
  if (!botId || !threadId || !resetsAt || resetsAt <= Date.now() || state === "set") return undefined;
  return {
    label: t("chat.limit.continueAt", { time: resetTime(resetsAt) }),
    onClick: () => {
      if (state === "working") return;
      setState("working");
      void api<{ routine?: Routine }>("/api/limit-resets/continue", { method: "POST", body: JSON.stringify({ botId, threadId }) })
        .then((response) => {
          if (response.routine) dispatch({ type: "routinePatched", routine: response.routine });
          setState("set");
        })
        .catch((cause) => {
          setState("idle");
          dispatch({ type: "error", message: cause instanceof Error ? cause.message : String(cause) });
        });
    },
  };
}

/** A usage-limit refusal whose reset time has passed stays in the transcript as evidence, collapsed, not as a current
 * blocker: the reset passing does not prove today's quota is available again. Null while the refusal is still current. */
export function usePastLimitNotice(tool: NonNullable<Message["tool"]>, messageAt: number | undefined) {
  const at = tool.limitResetsAt;
  const [clock, setClock] = useState(Date.now);
  useEffect(() => {
    if (typeof at !== "number" || !Number.isFinite(at)) return;
    let timer: ReturnType<typeof setTimeout> | undefined;
    const arm = () => {
      const remaining = at - Date.now();
      if (remaining <= 0) {
        setClock(Date.now());
        return;
      }
      timer = setTimeout(arm, Math.min(remaining + 1_000, 2_147_483_647));
    };
    arm();
    return () => { if (timer) clearTimeout(timer); };
  }, [at]);
  if (typeof at !== "number" || !Number.isFinite(at) || typeof messageAt !== "number" || messageAt > at || clock < at) return null;
  const { cause, notice } = failedTurnNotice(tool);
  if (!codexUsageLimitReached(cause)) return null;
  return (
    <details className="my-1 w-fit max-w-[min(var(--chat-bubble-max),var(--chat-bubble-pct))] rounded-lg border border-hairline/40 bg-raised px-3 py-2 text-[12px] text-ink-secondary">
      <summary className="cursor-pointer">{t("chat.error.pastUsageLimit")}</summary>
      <p className="mt-2 break-words">{notice ?? cause}</p>
    </details>
  );
}

function useEngineUsage(engine: string | undefined): EngineLimit | undefined {
  const limit = useEngineLimits().find((candidate) => candidate.engine === engine);
  return reportsUsage(limit) ? limit : undefined;
}

const BAR_STEP = 4;
const BAR_WIDTH = 2;
const GLYPH_HEIGHT = 14;
/** The bars end this far above the box's bottom, which puts their flat base on the text baseline. */
const BASELINE_INSET = 2;
const TALLEST = GLYPH_HEIGHT - BASELINE_INSET;
const GLYPH_WIDTH = BAR_STEP * USAGE_BARS - (BAR_STEP - BAR_WIDTH);
const TONE_INK: Record<UsageTone, string> = { fine: "text-ink-tertiary", near: "text-warning", out: "text-danger" };
const TONE_TRACK: Record<UsageTone, string> = { fine: "bg-ink-tertiary", near: "bg-warning", out: "bg-danger" };

/** Four rising bars on whole pixels, filled for the usage left; at the limit every bar is empty and struck through. */
export function UsageGlyph({ limit, className }: { limit: EngineLimit; className?: string }) {
  const filled = usageBars(limit);
  const tone = usageTone(limit);
  const step = Math.round(TALLEST / 2 / (USAGE_BARS - 1));
  return (
    <svg aria-hidden="true" data-usage-glyph={filled} width={GLYPH_WIDTH} height={GLYPH_HEIGHT} viewBox={`0 0 ${GLYPH_WIDTH} ${GLYPH_HEIGHT}`}
      className={cn("shrink-0", TONE_INK[tone], className)}>
      {Array.from({ length: USAGE_BARS }, (_, index) => {
        const height = TALLEST - (USAGE_BARS - 1 - index) * step;
        return <rect key={index} x={index * BAR_STEP} y={TALLEST - height} width={BAR_WIDTH} height={height} fill="currentColor" opacity={index < filled ? 1 : 0.3} />;
      })}
      {tone === "out" && <path d={`M0.75 ${TALLEST - 0.75}L${GLYPH_WIDTH - 0.75} 0.75`} stroke="currentColor" strokeWidth={1.5} strokeLinecap="round" />}
    </svg>
  );
}

const RING = 14;
const RING_STROKE = 2;
const RING_RADIUS = (RING - RING_STROKE) / 2;
const RING_LENGTH = 2 * Math.PI * RING_RADIUS;
const RING_INK: Record<UsageTone, string> = { fine: "text-success", near: "text-warning", out: "text-danger" };

/** A ring drawn clockwise from the top for the usage left; at the limit it is empty and struck through. */
export function UsageRing({ limit, className }: { limit: EngineLimit; className?: string }) {
  const tone = usageTone(limit);
  const left = tone === "out" ? 0 : Math.max(0, 100 - (limit.usedPercent ?? 0));
  const middle = RING / 2;
  return (
    <svg aria-hidden="true" data-usage-ring={tone} width={RING} height={RING} viewBox={`0 0 ${RING} ${RING}`} className={cn("shrink-0", RING_INK[tone], className)}>
      <circle cx={middle} cy={middle} r={RING_RADIUS} fill="none" stroke="currentColor" strokeWidth={RING_STROKE} opacity={0.3} />
      {left > 0 && <circle cx={middle} cy={middle} r={RING_RADIUS} fill="none" stroke="currentColor" strokeWidth={RING_STROKE} strokeLinecap="round"
        strokeDasharray={`${(RING_LENGTH * left) / 100} ${RING_LENGTH}`} transform={`rotate(-90 ${middle} ${middle})`} />}
      {tone === "out" && <path d={`M3 ${RING - 3}L${RING - 3} 3`} stroke="currentColor" strokeWidth={1.5} strokeLinecap="round" />}
    </svg>
  );
}

/** When the usage was last read, each window's share left with its reset and a track filled by use; at the limit, how it
 * clears and a fresh check. */
export function UsageRows({ limit }: { limit: EngineLimit }) {
  const [checking, setChecking] = useState(false);
  const dispatch = useStoreDispatch();
  const now = Date.now();
  const checkedAt = limit.checkedAt ?? limitsCheckedAt();
  return (
    <div data-usage-rows className="flex flex-col gap-2.5">
      <div className="flex items-baseline justify-between gap-3">
        <span className="min-w-0 truncate text-[13px] font-semibold leading-[18px] text-ink">{t("limits.cardTitle", { engine: limit.name })}</span>
        {checkedAt > 0 && <span data-usage-checked className="shrink-0 text-[12px] leading-4 text-ink-tertiary">{t("limits.checked", { when: threadUpdatedLabel(checkedAt, now) })}</span>}
      </div>
      {usageRows(limit).map((row) => (
        <div key={row.window} data-usage-row={row.window} className="flex flex-col gap-1.5">
          <div className="flex items-baseline gap-3 text-[12px] leading-4">
            <span className="shrink-0 text-ink-secondary">{windowLabel(row.window, limit.meter)}</span>
            <span className={cn("ms-auto min-w-0 truncate text-end tabular-nums", row.tone === "fine" ? "text-ink" : TONE_INK[row.tone])}>
              <span className="font-medium">{row.tone === "out" ? t("limits.windowAtLimit") : t("planUsage.left", { remaining: row.left })}</span>
              {row.resetsAt !== null && row.resetsAt > now && ` · ${t("limits.resets", { time: resetTime(row.resetsAt, now) })}`}
            </span>
          </div>
          <div aria-hidden="true" className="h-1 overflow-hidden rounded-full bg-ink/[0.08]">
            <div data-usage-track className={cn("h-full rounded-full", TONE_TRACK[row.tone])} style={{ width: `${100 - row.left}%` }} />
          </div>
        </div>
      ))}
      {limit.blocked && (
        <div className="flex items-start justify-between gap-3 border-t-[0.5px] border-ink/10 pt-2.5 text-[12px] leading-4">
          <p className="min-w-0 text-ink-tertiary">{t("limits.clearsOnReply", { engine: limit.name })}</p>
          <span className="flex shrink-0 flex-col items-end gap-1.5">
            <button type="button" data-usage-check disabled={checking} aria-busy={checking}
              onClick={() => { setChecking(true); void loadLimits(true).finally(() => setChecking(false)); }}
              className="font-medium text-ink underline underline-offset-2 hover:text-ink-secondary disabled:cursor-wait disabled:opacity-60">
              {t("common.checkAgain")}
            </button>
            {limit.held && <button type="button" data-usage-release disabled={checking} aria-busy={checking}
              onClick={() => {
                setChecking(true);
                void releaseLimit(limit.engine)
                  .catch((cause) => dispatch({ type: "error", message: cause instanceof Error ? cause.message : String(cause) }))
                  .finally(() => setChecking(false));
              }}
              className="font-medium text-ink underline underline-offset-2 hover:text-ink-secondary disabled:cursor-wait disabled:opacity-60">
              {t("limits.tryAgainNow")}
            </button>}
          </span>
        </div>
      )}
    </div>
  );
}

/** One AI's rows, the bot's own to begin with. When another connected AI reports usage too, a ring for each sits above
 * the rows, and pressing a ring shows that AI's rows. */
function UsageOverview({ engine }: { engine: string | undefined }) {
  const limits = useEngineLimits().filter(reportsUsage);
  const [picked, setPicked] = useState<string>();
  const shown = limits.find((limit) => limit.engine === picked) ?? limits.find((limit) => limit.engine === engine);
  const now = Date.now();
  return (
    <div className="flex flex-col gap-2.5">
      {limits.some((limit) => limit.engine !== engine) && (
        <div data-usage-rings role="group" aria-label={t("limits.title")} className="-mx-1.5 flex flex-wrap gap-1">
          {limits.map((limit) => (
            <button key={limit.engine} type="button" data-usage-engine={limit.engine} aria-pressed={limit === shown} aria-label={limitLine(limit, now, ", ")}
              title={limitLine(limit, now)} onClick={() => setPicked(limit.engine)}
              className={cn("flex h-6 min-w-0 items-center gap-1.5 rounded-full px-1.5 text-[12px] leading-4",
                limit === shown ? "bg-ink/[0.08] font-medium text-ink" : "text-ink-secondary hover:bg-ink/[0.06] hover:text-ink")}>
              <UsageRing limit={limit} />
              <span className="max-w-28 truncate">{limit.name}</span>
            </button>
          ))}
        </div>
      )}
      {shown && <UsageRows limit={shown} />}
    </div>
  );
}

const HOVER_OPEN_MS = 250;
const HOVER_CLOSE_MS = 150;
const CARD_WIDTH = 272;
const CARD_GAP = 8;
const keep = (event: SyntheticEvent) => event.stopPropagation();

function UsageCard({ limit, anchor, onEnter, onLeave }: { limit: EngineLimit; anchor: DOMRect; onEnter: () => void; onLeave: () => void }) {
  const below = anchor.top < window.innerHeight / 2;
  const style: CSSProperties = {
    position: "fixed", width: CARD_WIDTH,
    left: Math.max(CARD_GAP, Math.min(anchor.left + anchor.width / 2 - CARD_WIDTH / 2, window.innerWidth - CARD_WIDTH - CARD_GAP)),
    ...(below ? { top: anchor.bottom + CARD_GAP } : { bottom: window.innerHeight - anchor.top + CARD_GAP }),
  };
  // A portal still bubbles React events to the model button it was opened from, which would open the model menu.
  return (
    <div data-usage-card role="group" aria-label={limit.name} style={style} className="menu-surface z-[90] px-3 py-2.5"
      onPointerEnter={onEnter} onPointerLeave={onLeave} onPointerDown={keep} onMouseDown={keep} onClick={keep} onKeyDown={keep}>
      <UsageOverview engine={limit.engine} />
    </div>
  );
}

/** The usage left as bars inside the model button, with every window in a card on hover. `compact` hides the bars in
 * the narrow chat header while there is plenty left. The words are in the button's name for screen readers, and the
 * same rows open at the top of the model menu for keyboard and touch. */
export function UsageBars({ engine, compact = false, hover = true, className }: { engine: string | undefined; compact?: boolean; hover?: boolean; className?: string }) {
  const limit = useEngineUsage(engine);
  // The person may ask a bot for one ring instead (src/lib/composer-layout.ts); a click on it opens Spending.
  const ring = useComposerLayout().usage === "ring", dispatch = useStoreDispatch();
  const [anchor, setAnchor] = useState<DOMRect | null>(null);
  const spot = useRef<HTMLSpanElement>(null);
  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
  useEffect(() => () => clearTimeout(timer.current), []);
  if (!limit) return null;
  const tone = usageTone(limit);
  const later = (next: () => void, ms: number) => {
    clearTimeout(timer.current);
    timer.current = setTimeout(next, ms);
  };
  const enter = (event?: ReactPointerEvent) => {
    if (!hover || event?.pointerType === "touch" || spot.current?.closest("[aria-expanded=true]")) return;
    later(() => setAnchor(spot.current?.getBoundingClientRect() ?? null), anchor ? 0 : HOVER_OPEN_MS);
  };
  const leave = () => later(() => setAnchor(null), HOVER_CLOSE_MS);
  return (
    // An empty title keeps the button's own tooltip from opening over the card.
    <span ref={spot} title="" data-usage-bars={tone} data-usage-warn={tone === "fine" ? undefined : ""} className={cn("flex shrink-0 items-center", className)}
      onPointerEnter={enter} onPointerLeave={leave} onPointerDown={() => later(() => setAnchor(null), 0)}
      onClick={ring ? (event) => { event.preventDefault(); event.stopPropagation(); dispatch({ type: "toggleAppSettings", open: true, section: "usage" }); } : undefined}>
      {ring ? <UsageRing limit={limit} className={compact && tone === "fine" ? "@max-4xl/chathead:hidden" : undefined} />
        : <UsageGlyph limit={limit} className={compact && tone === "fine" ? "@max-4xl/chathead:hidden" : undefined} />}
      <span className="sr-only">{limitLine(limit, Date.now(), ", ")}</span>
      {anchor && createPortal(<UsageCard limit={limit} anchor={anchor} onEnter={() => enter()} onLeave={leave} />, document.body)}
    </span>
  );
}

/** Widens the narrow header's square model button to fit the bars while they warn. */
export const USAGE_WARN_WIDENS = "@max-4xl/chathead:has-[[data-usage-warn]]:w-auto @max-4xl/chathead:has-[[data-usage-warn]]:gap-1.5 @max-4xl/chathead:has-[[data-usage-warn]]:px-2";

/** The rows at the top of the open model menu, with a ring for every connected AI that reports usage. */
export function UsageMenuRows({ engine }: { engine: string | undefined }) {
  if (!useEngineLimits().some(reportsUsage)) return null;
  return <div data-usage-menu className="shrink-0 border-b-[0.5px] border-ink/10 px-3.5 py-2.5"><UsageOverview engine={engine} /></div>;
}

/** A phone's header has no model button: near or at the limit, the bars stand alone and open the bot's model. */
export function UsageAlert({ engine }: { engine: string | undefined }) {
  const dispatch = useStoreDispatch();
  const limit = useEngineUsage(engine);
  if (!limit || usageTone(limit) === "fine") return null;
  const line = limitLine(limit, Date.now(), ", ");
  return (
    <button type="button" data-usage-alert aria-label={line} title={line} onClick={() => openBotSettings(dispatch, "model")}
      className="flex size-9 shrink-0 items-center justify-center rounded-full border border-hairline bg-card">
      <UsageGlyph limit={limit} />
    </button>
  );
}