Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.1 KB
Raw Blame
"use client";

import { useEffect } from "react";
import { useAui, useAuiState } from "@assistant-ui/react";

/**
 * Keeps the per-message chain of thought aligned with the conversation preference.
 *
 * assistant-ui commits its chain resource in a passive effect. Updating it from a
 * layout effect races that commit and throws `Resource updated before mount` when
 * reasoning or tool parts first appear. Keep all programmatic chain updates in
 * passive effects so the resource is mounted before it receives an update.
 */
export const useAssistantReasoningState = (preferredCollapsed: boolean) => {
  const aui = useAui();
  const chainStatus = useAuiState((state) => state.chainOfThought.status);
  const collapsed = useAuiState((state) => state.chainOfThought.collapsed);

  useEffect(() => {
    aui.chainOfThought().setCollapsed(preferredCollapsed);
  }, [aui, preferredCollapsed]);

  useEffect(() => {
    if (chainStatus.type !== "requires-action") return;
    aui.chainOfThought().setCollapsed(false);
  }, [aui, chainStatus.type]);

  return { chainStatus, collapsed };
};