2026-08-22 02:18:27 +03:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
2026-10-10 09:43:12 +03:00
|
|
|
|
import { blurAfterPointerClick } from "@/components/focus-hygiene";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
import {
|
|
|
|
|
|
Collapsible,
|
|
|
|
|
|
CollapsibleContent,
|
|
|
|
|
|
CollapsibleTrigger,
|
|
|
|
|
|
} from "@/components/ui/collapsible";
|
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-10-02 02:09:38 +03:00
|
|
|
|
import { ChevronDownIcon } from "lucide-react";
|
|
|
|
|
|
import { BrailleSpinner } from "@/components/braille-spinner";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
import type { ComponentProps, ReactNode } from "react";
|
|
|
|
|
|
import {
|
|
|
|
|
|
createContext,
|
|
|
|
|
|
memo,
|
|
|
|
|
|
useCallback,
|
|
|
|
|
|
useContext,
|
|
|
|
|
|
useEffect,
|
|
|
|
|
|
useMemo,
|
|
|
|
|
|
useRef,
|
|
|
|
|
|
useState,
|
|
|
|
|
|
} from "react";
|
2026-08-22 09:42:39 +03:00
|
|
|
|
import { Streamdown } from "streamdown";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
2026-08-31 19:51:17 +03:00
|
|
|
|
import { transformMathDelimiters } from "@/src/core";
|
2026-09-19 13:16:34 +03:00
|
|
|
|
import { streamdownPlugins } from "./message";
|
|
|
|
|
|
import { mdComponents } from "@/components/md-blocks";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
import { Shimmer } from "./shimmer";
|
|
|
|
|
|
|
|
|
|
|
|
interface ReasoningContextValue {
|
|
|
|
|
|
isStreaming: boolean;
|
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
|
setIsOpen: (open: boolean) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ReasoningContext = createContext<ReasoningContextValue | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
export const useReasoning = () => {
|
|
|
|
|
|
const context = useContext(ReasoningContext);
|
|
|
|
|
|
if (!context) {
|
|
|
|
|
|
throw new Error("Reasoning components must be used within Reasoning");
|
|
|
|
|
|
}
|
|
|
|
|
|
return context;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type ReasoningProps = ComponentProps<typeof Collapsible> & {
|
|
|
|
|
|
isStreaming?: boolean;
|
|
|
|
|
|
open?: boolean;
|
|
|
|
|
|
defaultOpen?: boolean;
|
|
|
|
|
|
onOpenChange?: (open: boolean) => void;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const AUTO_CLOSE_DELAY = 1000;
|
|
|
|
|
|
|
|
|
|
|
|
export const Reasoning = memo(
|
|
|
|
|
|
({
|
|
|
|
|
|
className,
|
|
|
|
|
|
isStreaming = false,
|
|
|
|
|
|
open,
|
|
|
|
|
|
defaultOpen,
|
|
|
|
|
|
onOpenChange,
|
|
|
|
|
|
children,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: ReasoningProps) => {
|
2026-10-02 02:09:38 +03:00
|
|
|
|
// COLLAPSED BY DEFAULT, ALWAYS (owner, streaming round 2026-10-01):
|
|
|
|
|
|
// the old default opened on streaming and an auto-open effect
|
|
|
|
|
|
// re-fought a manual collapse — the block "wasn't collapsible until
|
|
|
|
|
|
// it finished". Collapsed is the default; opening is always the
|
|
|
|
|
|
// learner's click; the auto-close after the stream still tidies a
|
|
|
|
|
|
// row the learner had opened.
|
|
|
|
|
|
const resolvedDefaultOpen = defaultOpen ?? false;
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = useControllableState<boolean>({
|
|
|
|
|
|
defaultProp: resolvedDefaultOpen,
|
|
|
|
|
|
onChange: onOpenChange,
|
|
|
|
|
|
prop: open,
|
|
|
|
|
|
});
|
|
|
|
|
|
const hasEverStreamedRef = useRef(isStreaming);
|
|
|
|
|
|
const [hasAutoClosed, setHasAutoClosed] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
// Auto-close when streaming ends (once only, and only if it ever streamed)
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (
|
|
|
|
|
|
hasEverStreamedRef.current &&
|
|
|
|
|
|
!isStreaming &&
|
|
|
|
|
|
isOpen &&
|
|
|
|
|
|
!hasAutoClosed
|
|
|
|
|
|
) {
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
|
setHasAutoClosed(true);
|
|
|
|
|
|
}, AUTO_CLOSE_DELAY);
|
|
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [isStreaming, isOpen, setIsOpen, hasAutoClosed]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleOpenChange = useCallback(
|
|
|
|
|
|
(newOpen: boolean) => {
|
|
|
|
|
|
setIsOpen(newOpen);
|
|
|
|
|
|
},
|
|
|
|
|
|
[setIsOpen]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const contextValue = useMemo(
|
2026-10-05 03:41:39 +03:00
|
|
|
|
() => ({ isOpen, isStreaming, setIsOpen }),
|
|
|
|
|
|
[isOpen, isStreaming, setIsOpen]
|
2026-08-22 02:18:27 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<ReasoningContext.Provider value={contextValue}>
|
|
|
|
|
|
<Collapsible
|
|
|
|
|
|
className={cn("not-prose mb-4", className)}
|
|
|
|
|
|
onOpenChange={handleOpenChange}
|
|
|
|
|
|
open={isOpen}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
>
|
|
|
|
|
|
{children}
|
|
|
|
|
|
</Collapsible>
|
|
|
|
|
|
</ReasoningContext.Provider>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
export type ReasoningTriggerProps = ComponentProps<
|
|
|
|
|
|
typeof CollapsibleTrigger
|
|
|
|
|
|
> & {
|
2026-10-05 03:41:39 +03:00
|
|
|
|
getThinkingMessage?: (isStreaming: boolean) => ReactNode;
|
2026-08-22 02:18:27 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-10-05 03:41:39 +03:00
|
|
|
|
/* The settled label (owner round, 2026-10-05): static "Agent's thinking" —
|
|
|
|
|
|
the duration was live-turn state that died on remount anyway ("thought
|
|
|
|
|
|
for a few seconds"), bookkeeping for information we knowingly lose. The
|
|
|
|
|
|
label pairs with the user's collapsed "Your thinking" rows — one
|
|
|
|
|
|
language; motion (spinner + shimmer) stays the working signal. */
|
|
|
|
|
|
const defaultGetThinkingMessage = (isStreaming: boolean) => {
|
|
|
|
|
|
if (isStreaming) {
|
2026-08-22 02:18:27 +03:00
|
|
|
|
return <Shimmer duration={1}>Thinking...</Shimmer>;
|
|
|
|
|
|
}
|
2026-10-05 04:55:59 +03:00
|
|
|
|
return <p>AGENT’S THINKING</p>;
|
2026-08-22 02:18:27 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const ReasoningTrigger = memo(
|
|
|
|
|
|
({
|
|
|
|
|
|
className,
|
|
|
|
|
|
children,
|
|
|
|
|
|
getThinkingMessage = defaultGetThinkingMessage,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: ReasoningTriggerProps) => {
|
2026-10-05 03:41:39 +03:00
|
|
|
|
const { isStreaming, isOpen } = useReasoning();
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<CollapsibleTrigger
|
|
|
|
|
|
className={cn(
|
2026-10-05 04:43:08 +03:00
|
|
|
|
/* The heading register (owner round 4, 2026-10-05): the small
|
|
|
|
|
|
mono caps of the card eyebrows — matching YOUR THINKING, whose
|
|
|
|
|
|
rail offsets it by exactly the one-character difference. */
|
|
|
|
|
|
"flex w-full items-center gap-2 text-muted-foreground text-xs font-medium uppercase tracking-wide transition-colors hover:text-foreground",
|
2026-08-22 02:18:27 +03:00
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
{...props}
|
2026-10-10 09:43:12 +03:00
|
|
|
|
/* FOCUS HYGIENE (owner, keyboard round 2026-10-10): a pointer click
|
|
|
|
|
|
on the thinking toggle must not leave focus here — the learner's
|
|
|
|
|
|
next Enter submits, it must not re-toggle. Radix composes this
|
|
|
|
|
|
with its own toggle handler. */
|
|
|
|
|
|
onClick={(e) => blurAfterPointerClick(e)}
|
2026-08-22 02:18:27 +03:00
|
|
|
|
>
|
|
|
|
|
|
{children ?? (
|
|
|
|
|
|
<>
|
2026-10-02 02:09:38 +03:00
|
|
|
|
{/* The working-state grammar (owner, streaming round
|
|
|
|
|
|
2026-10-01): the Braille spinner replaces the brain icon
|
|
|
|
|
|
WHILE STREAMING ONLY — motion is the signal, so a finished
|
2026-10-02 03:09:22 +03:00
|
|
|
|
row carries no icon at all, just text + chevron. Size:
|
|
|
|
|
|
the component default (h-3) — IDENTICAL to the placeholder
|
|
|
|
|
|
spinner (owner, 2026-10-02): the handoff between the two
|
|
|
|
|
|
states must be visually seamless, one size everywhere. */}
|
|
|
|
|
|
{isStreaming && <BrailleSpinner label="Thinking" />}
|
2026-10-05 03:41:39 +03:00
|
|
|
|
{getThinkingMessage(isStreaming)}
|
2026-08-22 02:18:27 +03:00
|
|
|
|
<ChevronDownIcon
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"size-4 transition-transform",
|
|
|
|
|
|
isOpen ? "rotate-180" : "rotate-0"
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</CollapsibleTrigger>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
export type ReasoningContentProps = ComponentProps<
|
|
|
|
|
|
typeof CollapsibleContent
|
|
|
|
|
|
> & {
|
|
|
|
|
|
children: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-08-22 09:42:39 +03:00
|
|
|
|
// Shares message.tsx's Streamdown config (math delimiters, dark mermaid,
|
|
|
|
|
|
// remark-breaks) — reasoning renders the same contract as message text.
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
|
|
export const ReasoningContent = memo(
|
|
|
|
|
|
({ className, children, ...props }: ReasoningContentProps) => (
|
|
|
|
|
|
<CollapsibleContent
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"mt-4 text-sm",
|
|
|
|
|
|
"data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 text-muted-foreground outline-none data-[state=closed]:animate-out data-[state=open]:animate-in",
|
|
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
>
|
2026-09-19 13:16:34 +03:00
|
|
|
|
<Streamdown components={mdComponents} plugins={streamdownPlugins}>
|
2026-08-22 09:42:39 +03:00
|
|
|
|
{transformMathDelimiters(children)}
|
|
|
|
|
|
</Streamdown>
|
2026-08-22 02:18:27 +03:00
|
|
|
|
</CollapsibleContent>
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Reasoning.displayName = "Reasoning";
|
|
|
|
|
|
ReasoningTrigger.displayName = "ReasoningTrigger";
|
|
|
|
|
|
ReasoningContent.displayName = "ReasoningContent";
|