Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

19.9 KB
Raw Blame
import { useCompleteBrainDumpGreeting } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";
import { toast } from "@/components/molecules/Toast/use-toast";
import { useStableCallback } from "@/hooks/useStableCallback";
import { useAuth } from "@/lib/auth/hooks/useAuth";
import { isValidUUID } from "@/lib/utils";
import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";
import {
  peekGreetingDone,
  setGreetingDone,
  takeIntroAwaitingFollowup,
} from "@/services/onboarding/brain-dump-handoff";
import type { UIMessage } from "ai";
import { parseAsString, useQueryState } from "nuqs";
import { useEffect, useMemo, useRef, useState } from "react";
import { useCopilotStreamStore } from "./copilotStreamStore";
import {
  buildKickoffMessage,
  getKickoffAttemptToken,
  isKickoffMessage,
  shouldClearKickoffParam,
  type ExpertKickoffMetadata,
} from "./expertKickoff";
import { concatWithAssistantMerge } from "./helpers/convertChatSessionToUiMessages";
import {
  forgetHeldFollowUp,
  rememberHeldFollowUp,
  takeHeldFollowUps,
} from "./helpers/heldFollowUps";
import { queueFollowUpMessage } from "./helpers/queueFollowUpMessage";
import { stripReplayPrefix } from "./helpers/stripReplayPrefix";
import type { WorkspaceAttachment } from "./helpers/workspaceAttachments";
import { useCopilotUIStore } from "./store";
import { useChatSession } from "./useChatSession";
import { useCopilotNotifications } from "./useCopilotNotifications";
import { useCopilotPendingChips } from "./useCopilotPendingChips";
import { useCopilotRuntimeStream } from "./useCopilotRuntimeStream";
import { useCopilotStream } from "./useCopilotStream";
import { useExpertKickoff } from "./useExpertKickoff";
import { resolveExpertIdentity, useExpertMap } from "./useExpertMap";
import { useLoadMoreMessages } from "./useLoadMoreMessages";
import { recoverFailedDeferredSend, useSendMessage } from "./useSendMessage";
import { useSessionTitlePoll } from "./useSessionTitlePoll";
import { useSettleGate } from "./useSettleGate";
import { useWorkflowImportAutoSubmit } from "./useWorkflowImportAutoSubmit";

function trimVisibleMessagesForActiveRestore(messages: UIMessage[]) {
  const lastUserIndex = messages.findLastIndex(
    (message) => message.role === "user",
  );
  if (lastUserIndex === -1 || lastUserIndex === messages.length - 1) {
    return messages;
  }
  return messages.slice(0, lastUserIndex + 1);
}

function hasAssistantTail(messages: UIMessage[]) {
  const lastUserIndex = messages.findLastIndex(
    (message) => message.role === "user",
  );
  return lastUserIndex !== -1 && lastUserIndex < messages.length - 1;
}

function getLatestKickoffAttemptToken(messages: UIMessage[]) {
  for (let index = messages.length - 1; index >= 0; index -= 1) {
    const attemptToken = getKickoffAttemptToken(messages[index]);
    if (attemptToken) return attemptToken;
  }
  return null;
}

export function useCopilotPage({
  isStreamRuntime = false,
}: { isStreamRuntime?: boolean } = {}) {
  const { user, isUserLoading, isLoggedIn } = useAuth();

  const [expertIdParam] = useQueryState("expertId", parseAsString);
  const [newThreadParam] = useQueryState("new", parseAsString);
  const expertId = expertIdParam;
  const [kickoffParam, setKickoffParam] = useQueryState(
    "kickoff",
    parseAsString,
  );
  const {
    expertsById,
    isLoadingExperts,
    hasExpertsSettled,
    hasExpertsErrored,
  } = useExpertMap();
  const validExpertIdParam =
    expertIdParam && isValidUUID(expertIdParam) ? expertIdParam : null;
  // Day one only fires for an expert we can still address. `onKickoff` writes
  // its opening turn through `sendNewMessage`, bypassing the `onSend` archive
  // guard, so a bogus id, an unreadable roster or a fired expert has to fall
  // through to the read-only thread rather than reach the kickoff path.
  const kickoffExpert = validExpertIdParam
    ? expertsById.get(validExpertIdParam)
    : undefined;
  const kickoffExpertId =
    hasExpertsSettled && kickoffExpert?.isArchived === false
      ? kickoffExpert.id
      : null;
  const isKickoffResolving =
    kickoffParam === "1" && validExpertIdParam !== null && isLoadingExperts;

  useEffect(() => {
    if (kickoffParam !== "1") return;
    if (!shouldClearKickoffParam(hasExpertsSettled, kickoffExpertId)) return;
    void setKickoffParam(null, { history: "replace" });
  }, [hasExpertsSettled, kickoffExpertId, kickoffParam, setKickoffParam]);

  const copilotLlmModel = useCopilotUIStore((s) => s.copilotLlmModel);
  const isDryRun = useCopilotUIStore((s) => s.isDryRun);
  const setInitialPrompt = useCopilotUIStore((s) => s.setInitialPrompt);
  const { mutate: completeGreeting } = useCompleteBrainDumpGreeting();

  const {
    sessionId,
    setSessionId,
    sessionView,
    sessionLlmAuthProvider,
    sessionLlmCredentialId,
    chatLlmAuthProvider,
    sessionExpertId,
    isAdoptingExpertSession,
    hydratedMessages,
    rawSessionMessages,
    historicalTurnStats,
    activeTurnStartMessageId,
    hasActiveStream,
    activeStreamStartedAt,
    hasMoreMessages,
    oldestSequence,
    isLoadingSession,
    isSessionError,
    isSessionNotFound,
    createSession,
    isCreatingSession,
    refetchSession,
    sessionDryRun,
    sessionChatStatus,
    sessionSentFrom,
    sessionAutopilotMode,
  } = useChatSession({
    dryRun: isDryRun,
    expertId,
    adoptLatestExpertThread: !newThreadParam,
  });

  // An open session owns its identity: the URL param only describes who the
  // NEXT session will address, and it is absent whenever a thread is reached
  // from global search, a bookmark or a shared link.
  const activeExpertId = sessionId ? sessionExpertId : expertId;
  const expertIdentity = useMemo(
    () =>
      resolveExpertIdentity(activeExpertId, expertsById, {
        settled: hasExpertsSettled,
        errored: hasExpertsErrored,
      }),
    [activeExpertId, expertsById, hasExpertsErrored, hasExpertsSettled],
  );
  const isResolvingExpertIdentity = Boolean(
    activeExpertId && !hasExpertsSettled,
  );
  const isExpertSendLocked =
    isResolvingExpertIdentity || Boolean(expertIdentity?.isArchived);

  const streamArgs = {
    userId: user?.id ?? null,
    sessionId,
    sessionView,
    hydratedMessages,
    rawSessionMessages,
    sessionAuthProvider: sessionLlmAuthProvider,
    sessionCredentialId: sessionLlmCredentialId,
    activeTurnStartMessageId,
    activeTurnStartedAt: activeStreamStartedAt,
    hasActiveStream,
    refetchSession,
    // Sent whenever the picker can set it. The tier control is not behind
    // CHAT_MODE_OPTION -- it renders from the server's connection offer --
    // so gating the value on that flag silently ran the turn on the tier the
    // user had not chosen. Entitlement is the server's call, not the flag's.
    copilotModel: copilotLlmModel,
  };
  // The host keys its mount by the flag, so this never changes within one.
  const useStream = isStreamRuntime
    ? useCopilotRuntimeStream
    : useCopilotStream;
  const stream = useStream(streamArgs);
  const runtimeStream = "appendLocalUserRows" in stream ? stream : null;
  const {
    followBackendTurn,
    messages: currentMessages,
    sendMessage,
    stop,
    status,
    error,
    isReconnecting,
    isFinishProbing,
    isRestoringActiveSession,
    isUserStoppingRef,
    isUserStopping,
    rateLimitMessage,
    platformLimitFailure,
    dismissRateLimit,
    providerLimit,
    dismissProviderLimit,
  } = stream;
  const kickoffAttemptToken = getLatestKickoffAttemptToken(currentMessages);

  const { pagedMessages, pagedTurnStats, hasMore, isLoadingMore, loadMore } =
    useLoadMoreMessages({
      sessionId,
      initialOldestSequence: runtimeStream
        ? runtimeStream.oldestSequence
        : oldestSequence,
      initialHasMore: hasMoreMessages,
      initialPageRawMessages: rawSessionMessages,
    });

  // Merge the older-pages and current-page stat maps; current-page (historical)
  // wins on overlap since it was persisted more recently.
  const currentTurnStats = runtimeStream
    ? runtimeStream.turnStats
    : historicalTurnStats;
  const turnStats = useMemo(() => {
    const merged = new Map(pagedTurnStats);
    currentTurnStats?.forEach((v, k) => merged.set(k, v));
    return merged;
  }, [pagedTurnStats, currentTurnStats]);

  // Ref that mirrors whether a stream turn is currently in-flight.
  // Updated synchronously on every render so it always reflects the latest
  // status — unlike reading `status` inside onSend (which captures the
  // closure's render-cycle value and can be stale for a frame).
  // Setting it to true *before* calling sendMessage prevents rapid
  // double-presses from both routing to /stream before React can re-render
  // with status="submitted".
  const isInflightRef = useRef(false);
  isInflightRef.current =
    !isUserStopping && (status === "streaming" || status === "submitted");

  // Whether this tab has finished drawing the previous turn. The server's
  // turn can end well before the screen does: the smoothing transform paces
  // text out word by word (copilotStreamSmoothing.ts), and the post-finish
  // probe may still turn into a reconnect. A follow-up the backend refused
  // to queue (409, no active turn) waits here before going out as a new
  // turn — see onSend.
  const isLocalStreamSettled =
    !isInflightRef.current && !isFinishProbing && !isReconnecting;
  // A held follow-up learns whether the stream settled (send now) or the
  // hook went away first (the chat host unmounts on a chat switch and on
  // reload; the follow-up then stays in sessionStorage, see below).
  const { waitUntilSettled: waitForLocalSettle, markUnsettled } =
    useSettleGate(isLocalStreamSettled);

  // Follow-ups waiting on the settle above. Shown as "Queued" chips beside
  // the backend-buffered ones, since from the user's side they are the same
  // thing. Mirrored into sessionStorage: this host is unmounted on a chat
  // switch and gone on a reload, and the only copy was in memory.
  const [heldFollowUps, setHeldFollowUps] = useState<string[]>([]);
  function holdFollowUp(forSessionId: string, text: string) {
    setHeldFollowUps((prev) => [...prev, text]);
    rememberHeldFollowUp(forSessionId, text);
  }
  function releaseFollowUp(forSessionId: string, text: string) {
    setHeldFollowUps((prev) => {
      const index = prev.indexOf(text);
      return index === -1 ? prev : prev.filter((_, i) => i !== index);
    });
    forgetHeldFollowUp(forSessionId, text);
  }
  useEffect(() => {
    if (!sessionId) return;
    const leftBehind = takeHeldFollowUps(sessionId);
    if (leftBehind.length === 0) return;
    setInitialPrompt(leftBehind.join("\n\n"));
    toast({
      title: "Follow-up not sent",
      description:
        "You left the chat before it could go out. It's back in the composer.",
    });
  }, [sessionId, setInitialPrompt]);

  // Combine paginated messages with current page messages, merging consecutive
  // assistant UIMessages at the page boundary so reasoning + response parts
  // stay in a single bubble. Paged messages are older history prepended before
  // the current page.
  // Memoised on their inputs so the stripReplayPrefix memos below only re-run
  // when the messages actually change, not on every render once older pages
  // are loaded.
  const rawMessages = useMemo(
    () => concatWithAssistantMerge(pagedMessages, currentMessages),
    [pagedMessages, currentMessages],
  );
  const cachedSessionMessages = useMemo(
    () =>
      sessionId
        ? useCopilotStreamStore.getState().getMessageSnapshot(sessionId)
        : [],
    [sessionId],
  );
  const cachedRawMessages = useMemo(
    () => concatWithAssistantMerge(pagedMessages, cachedSessionMessages),
    [pagedMessages, cachedSessionMessages],
  );

  // Drop / trim assistant messages whose leading text is a replay of an
  // earlier assistant (Claude Agent SDK's `--resume` behaviour). See
  // helpers/stripReplayPrefix.ts for the three cases.
  const messages = useMemo(() => stripReplayPrefix(rawMessages), [rawMessages]);
  const cachedMessages = useMemo(
    () => stripReplayPrefix(cachedRawMessages),
    [cachedRawMessages],
  );
  // The runtime's restore state is a passive notice; it never trims the list.
  const restoreStatusMessage = runtimeStream?.restoreStatusMessage ?? null;
  const displayMessages = useMemo(() => {
    if (!isRestoringActiveSession || runtimeStream) return messages;
    if (hasAssistantTail(cachedMessages)) return cachedMessages;
    return trimVisibleMessagesForActiveRestore(messages);
  }, [isRestoringActiveSession, runtimeStream, messages, cachedMessages]);

  // Chip state machine (peek sync + auto-continue promotion + mid-turn poll)
  // lives in a dedicated hook so this component is just glue.
  const { queuedMessages, queueMessage } = useCopilotPendingChips({
    sessionId,
    status,
    messages,
    ...("setMessages" in stream
      ? { setMessages: stream.setMessages }
      : {
          appendLocalUserRows: stream.appendLocalUserRows,
          drainedCount: stream.drainedCount,
        }),
  });

  useCopilotNotifications(sessionId);

  const {
    onSend: sendNewMessage,
    isUploadingFiles,
    pendingSend,
    setPendingFileParts,
  } = useSendMessage({
    sessionId,
    sendMessage,
    createSession,
    isUserStoppingRef,
  });

  // Wrap sendNewMessage with queue-in-flight routing: if a session is active
  // and a turn is already running, POST the follow-up text to the pending
  // endpoint so the backend buffers it; otherwise fall through to normal send.
  async function sendOrQueue(
    message: string,
    files?: File[],
    workspaceFiles?: WorkspaceAttachment[],
    metadata?: ExpertKickoffMetadata,
  ) {
    const trimmed = message.trim();
    const hasAttachments =
      (files?.length ?? 0) > 0 || (workspaceFiles?.length ?? 0) > 0;
    if (!trimmed && !hasAttachments) return;
    if (isExpertSendLocked) return;

    // Sending anything retires the greeting for good: flag it done on
    // the server (kept in the DB, just never shown again) and cache the
    // fact locally so no future visit even has to ask.
    if (!peekGreetingDone(user?.id)) {
      completeGreeting(undefined, {
        onSuccess: () => setGreetingDone(user?.id),
        // Retiring the greeting is bookkeeping — the worst case is
        // seeing it once more — so a failure must never surface here.
        onError: () => undefined,
      });
    }
    if (takeIntroAwaitingFollowup()) {
      trackBrainDump("intro_followup_sent", { chars: trimmed.length });
    }

    let heldForLocalSettle = false;
    // A loop, not an `if`: a held follow-up re-checks the in-flight ref once
    // the screen settles, because another held follow-up may have dispatched
    // a new turn in the same settle (in which case this one queues behind
    // it) — and never sends while a turn is still being drawn.
    while (sessionId && isInflightRef.current) {
      if (hasAttachments) {
        toast({
          title: "Please wait to attach files",
          description:
            "File attachments can't be queued until the current response finishes.",
          variant: "destructive",
        });
        return;
      }

      try {
        await queueFollowUpMessage(sessionId, trimmed);
        if (heldForLocalSettle) releaseFollowUp(sessionId, trimmed);
        queueMessage(trimmed);
        return;
      } catch (err) {
        // Any other failure propagates to the composer, which restores the
        // draft and shows the one toast for it.
        if (
          !(err instanceof Error && err.name === "QueueFollowUpNotActiveError")
        ) {
          if (heldForLocalSettle) releaseFollowUp(sessionId, trimmed);
          throw err;
        }
      }

      // The backend's turn is already over, but this tab may still be
      // drawing it. Starting a second `useChat` request now cuts the live
      // answer off mid-sentence: AI SDK only streams into the last message
      // while its id matches, and the new user bubble takes that slot (see
      // midTurnSplit.ts). Hold the follow-up until the local stream has
      // settled, then send it as a normal turn below the finished answer.
      if (!heldForLocalSettle) holdFollowUp(sessionId, trimmed);
      heldForLocalSettle = true;
      if (!(await waitForLocalSettle())) return;
    }

    // Mark in-flight synchronously before dispatching so a rapid second
    // press sees isInflightRef.current=true and routes to the queue path
    // instead of triggering a duplicate /stream POST.
    if (sessionId) {
      isInflightRef.current = true;
      markUnsettled();
    }
    if (heldForLocalSettle && sessionId) {
      releaseFollowUp(sessionId, trimmed);
      // Resolve once dispatched, not when the whole answer has streamed:
      // the composer's enqueue path is waiting on this, and holding it for
      // the entire turn would lock Enter and the queue button again.
      void sendNewMessage(message, files, workspaceFiles, metadata).catch(
        (err: unknown) => recoverFailedDeferredSend(trimmed, [], err),
      );
      return;
    }
    return await sendNewMessage(message, files, workspaceFiles, metadata);
  }
  // One function for the host's lifetime that runs the latest `sendOrQueue`,
  // so the chat actions and the composer keep their props across flushes.
  const onSend = useStableCallback(
    async (...args: Parameters<typeof sendOrQueue>) => {
      await sendOrQueue(...args);
    },
  );
  // A suggested task records which chat its message started.
  const onStartTask = useStableCallback((message: string) =>
    sendOrQueue(message),
  );

  useWorkflowImportAutoSubmit({
    // This render's function on purpose: its effect lists `onSend` as a
    // dependency and re-checks the URL for a prompt every time it changes.
    onSend: sendOrQueue,
    setPendingFileParts,
    isSendLocked: isExpertSendLocked,
  });

  const { isKickoffStarting } = useExpertKickoff({
    userId: user?.id ?? null,
    expertId: kickoffExpertId,
    kickoff: kickoffParam === "1",
    sessionId,
    sessionExpertId,
    hasPersistedExpertHistory:
      sessionId && hydratedMessages !== undefined
        ? hydratedMessages.some((message) => !isKickoffMessage(message))
        : null,
    kickoffAttemptToken,
    isClientThreadEmpty: messages.every(isKickoffMessage),
    onAdoptSession: setSessionId,
    async onKickoff(id, attemptToken) {
      const kickoffMessage = buildKickoffMessage(id, attemptToken);
      await sendNewMessage(
        kickoffMessage.text,
        undefined,
        undefined,
        kickoffMessage.metadata,
      );
    },
    onSettled() {
      void setKickoffParam(null, { history: "replace" });
    },
  });

  useSessionTitlePoll({ sessionId, status, isReconnecting });

  return {
    sessionId,
    messages: displayMessages,
    status,
    error,
    stop,
    isReconnecting,
    isFinishProbing,
    isRestoringActiveSession,
    restoreStatusMessage,
    activeStreamStartedAt,
    isUserStopping,
    isLoadingSession,
    isSessionError,
    isSessionNotFound,
    isCreatingSession,
    isUploadingFiles,
    pendingSend,
    isUserLoading,
    isLoggedIn,
    createSession,
    onSend,
    onStartTask,
    // onEnqueue delegates to onSend, which internally routes to the queue
    // endpoint when isInflightRef.current is true.
    onEnqueue: onSend,
    queuedMessages:
      heldFollowUps.length > 0
        ? [...queuedMessages, ...heldFollowUps]
        : queuedMessages,
    hasMoreMessages: hasMore,
    isLoadingMore,
    loadMore,
    turnStats,
    rateLimitMessage,
    platformLimitFailure,
    dismissRateLimit,
    providerLimit,
    dismissProviderLimit,
    // sessionDryRun is the CURRENT session's immutable dry_run flag from API,
    // used to render the banner. The global `isDryRun` preference (for new
    // sessions) lives in the store and is consumed by the toggle button.
    sessionDryRun,
    sessionChatStatus,
    sessionSentFrom,
    sessionAutopilotMode,
    chatLlmAuthProvider,
    expertIdentity,
    isResolvingExpertIdentity,
    isAdoptingExpertSession,
    isKickoffStarting: isKickoffResolving || isKickoffStarting,
    followBackendTurn,
  };
}