Skip to content

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

5.5 KB
Raw Blame
// What each thread's running turn is doing right now, read from the live runtime frames: the engine's own report of a
// tool call (turn.progress), reasoning or reply text streaming, and the turn's end. Held per thread outside the app
// reducer, so a frame re-renders only the chat that shows that thread, and only when what it shows changes.
import { useSyncExternalStore } from "react";

import type { RuntimeEvent } from "../../shared/runtime-events";
import { progressKey, type TurnProgressActivity, type TurnProgressFields } from "../../shared/turn-progress";

export type LiveStatus =
  | ({ activity: TurnProgressActivity } & Omit<TurnProgressFields, "type" | "activity">)
  | { activity: "thinking" | "reply" | "summarizing"; itemId?: undefined };

export interface TurnLive {
  turnId?: string;
  /** The newest turn this thread showed has completed, failed or stopped. */
  ended: boolean;
  status: LiveStatus | null;
  /** A summary started after this turn had ended (the Compact button); its end puts the ended state back. */
  reopened?: true;
}

const UNKNOWN: TurnLive = { ended: false, status: null };
const MAX_THREADS = 500;
const threads = new Map<string, TurnLive>();
const lastFrames = new Map<string, number>();
const listeners = new Map<string, Set<() => void>>();

function sameStatus(a: LiveStatus | null, b: LiveStatus | null): boolean {
  if (a === b) return true;
  if (!a || !b) return false;
  return ("bytes" in a ? a.bytes : undefined) === ("bytes" in b ? b.bytes : undefined) && progressKey(a) === progressKey(b);
}

function put(threadId: string, next: TurnLive): void {
  const current = threads.get(threadId);
  if (current && current.turnId === next.turnId && current.ended === next.ended && sameStatus(current.status, next.status)) return;
  threads.delete(threadId);
  threads.set(threadId, next);
  if (threads.size > MAX_THREADS) threads.delete(threads.keys().next().value!);
  for (const listener of Array.from(listeners.get(threadId) ?? [])) listener();
}

/** One live runtime frame. A row in the chat supersedes the status it came from, except the row of the very tool call
 * the status describes: "Writing index.html · 14 KB" outlasts the call's own chip until the call finishes. */
export function noteTurnEvent(event: RuntimeEvent): void {
  const { threadId } = event;
  lastFrames.delete(threadId);
  lastFrames.set(threadId, Date.now());
  if (lastFrames.size > MAX_THREADS) lastFrames.delete(lastFrames.keys().next().value!);
  const current = threads.get(threadId);
  const sameTurn = current && (!event.turnId || event.turnId === current.turnId);
  const turnId = event.turnId ?? current?.turnId;
  const live: TurnLive = sameTurn ? current : { turnId, ended: false, status: null };
  switch (event.type) {
    case "turn.started":
      return put(threadId, { turnId, ended: false, status: null });
    case "turn.completed":
    case "cap.exhausted":
    case "session.exited":
      return put(threadId, { turnId, ended: true, status: null });
    case "turn.progress": {
      const { activity, tool, file, bytes, itemId, target, detail, quiet, tail } = event;
      return put(threadId, { turnId, ended: false, status: {
        activity, ...(tool ? { tool } : {}), ...(file ? { file } : {}), ...(bytes !== undefined ? { bytes } : {}), ...(itemId ? { itemId } : {}),
        ...(target ? { target } : {}), ...(detail ? { detail } : {}), ...(quiet ? { quiet } : {}), ...(tail?.length ? { tail } : {}),
      } });
    }
    case "content.delta":
      return put(threadId, { turnId, ended: false, status: { activity: event.streamKind === "reasoning_text" ? "thinking" : "reply" } });
    case "item.started":
      return put(threadId, { ...live, ended: false, status: live.status?.itemId && live.status.itemId === event.itemId ? live.status : null });
    case "item.completed":
    case "request.opened":
      return put(threadId, { ...live, status: null });
    case "context.summarizing": {
      if (event.active) return put(threadId, { ...live, ended: false, status: { activity: "summarizing" }, ...(live.ended ? { reopened: true as const } : {}) });
      const { reopened, ...rest } = live;
      const stillSummarizing = rest.status?.activity === "summarizing";
      return put(threadId, { ...rest, ended: rest.ended || (!!reopened && stillSummarizing), status: stillSummarizing ? null : rest.status });
    }
    default:
      if (live !== current) put(threadId, live);
  }
}

export function turnLive(threadId: string | null | undefined): TurnLive {
  return (threadId && threads.get(threadId)) || UNKNOWN;
}

/** When this thread last sent any live frame, or, before its first one, when this window first asked. Read on a clock
 * rather than subscribed, so streamed text never re-renders. */
export function silentSince(threadId: string): number {
  const at = lastFrames.get(threadId);
  if (at !== undefined) return at;
  const now = Date.now();
  lastFrames.set(threadId, now);
  return now;
}

function subscribe(threadId: string | null | undefined, listener: () => void): () => void {
  if (!threadId) return () => {};
  let set = listeners.get(threadId);
  if (!set) listeners.set(threadId, (set = new Set()));
  set.add(listener);
  return () => {
    set.delete(listener);
    if (set.size === 0) listeners.delete(threadId);
  };
}

export function useTurnLive(threadId: string | null | undefined): TurnLive {
  return useSyncExternalStore(
    (listener) => subscribe(threadId, listener),
    () => turnLive(threadId),
    () => turnLive(threadId),
  );
}

/** Tests only. */
export function resetTurnLive(): void {
  threads.clear();
  lastFrames.clear();
}