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.

3.5 KB
Raw Blame
import { postV2CancelSessionTask } from "@/app/api/__generated__/endpoints/chat/chat";
import { toast } from "@/components/molecules/Toast/use-toast";
import type { UIMessage } from "ai";
import { resolveInProgressTools } from "./helpers";

/**
 * User-visible marker appended to the last assistant message so the UI
 * renders a "You manually stopped this chat" row the moment the SSE is
 * aborted. The backend writes the same marker to the DB, so a subsequent
 * reload shows the same state. Must match `COPILOT_ERROR_PREFIX` in
 * `ChatMessagesContainer/helpers.ts`.
 */
export const CANCELLED_MARKER = "[__COPILOT_ERROR_f7a1__] Operation cancelled";

interface UseCopilotStopArgs {
  sessionId: string | null;
  /** Aborts the stream and marks the turn stopped on screen, synchronously,
   *  so the UI reflects the click before the cancel POST returns. */
  stopStream: () => void;
  /** Flipped to `true` so the stream's onError/onFinish callbacks don't
   *  misinterpret the resulting AbortError as a disconnect + reconnect. */
  isUserStoppingRef: React.MutableRefObject<boolean>;
  /** State setter mirroring ``isUserStoppingRef`` — drives the UI so the
   *  stop-button UX flips immediately instead of waiting for AI SDK's
   *  ``status`` to transition away from "streaming" on abort. */
  setIsUserStopping: (value: boolean) => void;
}

/**
 * Build the `stop` handler for the chat stream:
 *   1. flag the stop as user-initiated (so onError/onFinish don't reconnect)
 *   2. abort the stream and mark the turn stopped, for instant UI feedback
 *   3. asynchronously tell the backend executor to actually stop the task,
 *      surfacing a toast when the cancel was published but not yet
 *      confirmed (the task should stop shortly) or failed outright.
 */
export function useCopilotStop({
  sessionId,
  stopStream,
  isUserStoppingRef,
  setIsUserStopping,
}: UseCopilotStopArgs) {
  async function stop() {
    isUserStoppingRef.current = true;
    setIsUserStopping(true);
    stopStream();

    if (!sessionId) return;
    try {
      const res = await postV2CancelSessionTask(sessionId);
      if (
        res.status === 200 &&
        "reason" in res.data &&
        res.data.reason === "cancel_published_not_confirmed"
      ) {
        toast({
          title: "Stop may take a moment",
          description:
            "The cancel was sent but not yet confirmed. The task should stop shortly.",
        });
      }
    } catch {
      toast({
        title: "Could not stop the task",
        description: "The task may still be running in the background.",
        variant: "destructive",
      });
    }
  }

  return stop;
}

/**
 * The AI SDK path's `stopStream`: abort the SDK's fetch, cancel open tools
 * and append the marker to the last assistant message.
 */
export function sdkStopStream(
  sdkStop: () => void,
  setMessages: (updater: (prev: UIMessage[]) => UIMessage[]) => void,
) {
  return () => {
    try {
      sdkStop();
    } catch {
      // sdkStop throws if no fetch is in flight — the user-stop flag
      // already flipped, so the UI reflects the intent either way.
    }
    setMessages(markMessagesStopped);
  };
}

function markMessagesStopped(prev: UIMessage[]): UIMessage[] {
  const resolved = resolveInProgressTools(prev, "cancelled");
  const last = resolved[resolved.length - 1];
  if (last?.role !== "assistant") return resolved;
  return [
    ...resolved.slice(0, -1),
    {
      ...last,
      parts: [...last.parts, { type: "text" as const, text: CANCELLED_MARKER }],
    },
  ];
}