2026-08-22 02:18:27 +03:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
ButtonGroup,
|
|
|
|
|
ButtonGroupText,
|
|
|
|
|
} from "@/components/ui/button-group";
|
|
|
|
|
import {
|
|
|
|
|
Tooltip,
|
|
|
|
|
TooltipContent,
|
|
|
|
|
TooltipProvider,
|
|
|
|
|
TooltipTrigger,
|
|
|
|
|
} from "@/components/ui/tooltip";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { cjk } from "@streamdown/cjk";
|
|
|
|
|
import { code } from "@streamdown/code";
|
2026-08-22 03:23:11 +03:00
|
|
|
import { createMathPlugin } from "@streamdown/math";
|
2026-09-26 01:52:47 +03:00
|
|
|
|
2026-08-22 02:18:27 +03:00
|
|
|
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
|
|
|
|
|
import type { ComponentProps, HTMLAttributes, ReactElement } from "react";
|
2026-09-26 01:52:47 +03:00
|
|
|
import type { LhViewMessage } from "@/src/core";
|
2026-08-22 02:18:27 +03:00
|
|
|
import {
|
|
|
|
|
createContext,
|
|
|
|
|
memo,
|
|
|
|
|
useCallback,
|
|
|
|
|
useContext,
|
|
|
|
|
useEffect,
|
|
|
|
|
useMemo,
|
|
|
|
|
useState,
|
|
|
|
|
} from "react";
|
2026-08-22 09:38:54 +03:00
|
|
|
import remarkBreaks from "remark-breaks";
|
|
|
|
|
import {
|
|
|
|
|
Streamdown,
|
|
|
|
|
type PluginConfig,
|
|
|
|
|
} from "streamdown";
|
2026-09-19 13:16:34 +03:00
|
|
|
import { mdComponents } from "@/components/md-blocks";
|
2026-08-31 19:51:17 +03:00
|
|
|
import { transformMathDelimiters } from "@/src/core";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
export type MessageProps = HTMLAttributes<HTMLDivElement> & {
|
2026-09-26 01:52:47 +03:00
|
|
|
from: LhViewMessage["role"];
|
2026-08-22 02:18:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const Message = ({ className, from, ...props }: MessageProps) => (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"group flex w-full max-w-[95%] flex-col gap-2",
|
|
|
|
|
from === "user" ? "is-user ml-auto justify-end" : "is-assistant",
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type MessageContentProps = HTMLAttributes<HTMLDivElement>;
|
|
|
|
|
|
|
|
|
|
export const MessageContent = ({
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageContentProps) => (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"is-user:dark flex w-fit min-w-0 max-w-full flex-col gap-2 overflow-hidden text-sm",
|
|
|
|
|
"group-[.is-user]:ml-auto group-[.is-user]:rounded-lg group-[.is-user]:bg-secondary group-[.is-user]:px-4 group-[.is-user]:py-3 group-[.is-user]:text-foreground",
|
|
|
|
|
"group-[.is-assistant]:text-foreground",
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type MessageActionsProps = ComponentProps<"div">;
|
|
|
|
|
|
|
|
|
|
export const MessageActions = ({
|
|
|
|
|
className,
|
|
|
|
|
children,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageActionsProps) => (
|
|
|
|
|
<div className={cn("flex items-center gap-1", className)} {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type MessageActionProps = ComponentProps<typeof Button> & {
|
|
|
|
|
tooltip?: string;
|
|
|
|
|
label?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const MessageAction = ({
|
|
|
|
|
tooltip,
|
|
|
|
|
children,
|
|
|
|
|
label,
|
|
|
|
|
variant = "ghost",
|
|
|
|
|
size = "icon-sm",
|
|
|
|
|
...props
|
|
|
|
|
}: MessageActionProps) => {
|
|
|
|
|
const button = (
|
|
|
|
|
<Button size={size} type="button" variant={variant} {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
<span className="sr-only">{label || tooltip}</span>
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (tooltip) {
|
|
|
|
|
return (
|
|
|
|
|
<TooltipProvider>
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>{button}</TooltipTrigger>
|
|
|
|
|
<TooltipContent>
|
|
|
|
|
<p>{tooltip}</p>
|
|
|
|
|
</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</TooltipProvider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return button;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface MessageBranchContextType {
|
|
|
|
|
currentBranch: number;
|
|
|
|
|
totalBranches: number;
|
|
|
|
|
goToPrevious: () => void;
|
|
|
|
|
goToNext: () => void;
|
|
|
|
|
branches: ReactElement[];
|
|
|
|
|
setBranches: (branches: ReactElement[]) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MessageBranchContext = createContext<MessageBranchContextType | null>(
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const useMessageBranch = () => {
|
|
|
|
|
const context = useContext(MessageBranchContext);
|
|
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"MessageBranch components must be used within MessageBranch"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageBranchProps = HTMLAttributes<HTMLDivElement> & {
|
|
|
|
|
defaultBranch?: number;
|
|
|
|
|
onBranchChange?: (branchIndex: number) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const MessageBranch = ({
|
|
|
|
|
defaultBranch = 0,
|
|
|
|
|
onBranchChange,
|
|
|
|
|
className,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageBranchProps) => {
|
|
|
|
|
const [currentBranch, setCurrentBranch] = useState(defaultBranch);
|
|
|
|
|
const [branches, setBranches] = useState<ReactElement[]>([]);
|
|
|
|
|
|
|
|
|
|
const handleBranchChange = useCallback(
|
|
|
|
|
(newBranch: number) => {
|
|
|
|
|
setCurrentBranch(newBranch);
|
|
|
|
|
onBranchChange?.(newBranch);
|
|
|
|
|
},
|
|
|
|
|
[onBranchChange]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const goToPrevious = useCallback(() => {
|
|
|
|
|
const newBranch =
|
|
|
|
|
currentBranch > 0 ? currentBranch - 1 : branches.length - 1;
|
|
|
|
|
handleBranchChange(newBranch);
|
|
|
|
|
}, [currentBranch, branches.length, handleBranchChange]);
|
|
|
|
|
|
|
|
|
|
const goToNext = useCallback(() => {
|
|
|
|
|
const newBranch =
|
|
|
|
|
currentBranch < branches.length - 1 ? currentBranch + 1 : 0;
|
|
|
|
|
handleBranchChange(newBranch);
|
|
|
|
|
}, [currentBranch, branches.length, handleBranchChange]);
|
|
|
|
|
|
|
|
|
|
const contextValue = useMemo<MessageBranchContextType>(
|
|
|
|
|
() => ({
|
|
|
|
|
branches,
|
|
|
|
|
currentBranch,
|
|
|
|
|
goToNext,
|
|
|
|
|
goToPrevious,
|
|
|
|
|
setBranches,
|
|
|
|
|
totalBranches: branches.length,
|
|
|
|
|
}),
|
|
|
|
|
[branches, currentBranch, goToNext, goToPrevious]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<MessageBranchContext.Provider value={contextValue}>
|
|
|
|
|
<div
|
|
|
|
|
className={cn("grid w-full gap-2 [&>div]:pb-0", className)}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
</MessageBranchContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageBranchContentProps = HTMLAttributes<HTMLDivElement>;
|
|
|
|
|
|
|
|
|
|
export const MessageBranchContent = ({
|
|
|
|
|
children,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageBranchContentProps) => {
|
|
|
|
|
const { currentBranch, setBranches, branches } = useMessageBranch();
|
|
|
|
|
const childrenArray = useMemo(
|
|
|
|
|
() => (Array.isArray(children) ? children : [children]),
|
|
|
|
|
[children]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Use useEffect to update branches when they change
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (branches.length !== childrenArray.length) {
|
|
|
|
|
setBranches(childrenArray);
|
|
|
|
|
}
|
|
|
|
|
}, [childrenArray, branches, setBranches]);
|
|
|
|
|
|
|
|
|
|
return childrenArray.map((branch, index) => (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"grid gap-2 overflow-hidden [&>div]:pb-0",
|
|
|
|
|
index === currentBranch ? "block" : "hidden"
|
|
|
|
|
)}
|
|
|
|
|
key={branch.key}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{branch}
|
|
|
|
|
</div>
|
|
|
|
|
));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageBranchSelectorProps = ComponentProps<typeof ButtonGroup>;
|
|
|
|
|
|
|
|
|
|
export const MessageBranchSelector = ({
|
|
|
|
|
className,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageBranchSelectorProps) => {
|
|
|
|
|
const { totalBranches } = useMessageBranch();
|
|
|
|
|
|
|
|
|
|
// Don't render if there's only one branch
|
|
|
|
|
if (totalBranches <= 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ButtonGroup
|
|
|
|
|
className={cn(
|
|
|
|
|
"[&>*:not(:first-child)]:rounded-l-md [&>*:not(:last-child)]:rounded-r-md",
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
orientation="horizontal"
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageBranchPreviousProps = ComponentProps<typeof Button>;
|
|
|
|
|
|
|
|
|
|
export const MessageBranchPrevious = ({
|
|
|
|
|
children,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageBranchPreviousProps) => {
|
|
|
|
|
const { goToPrevious, totalBranches } = useMessageBranch();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
aria-label="Previous branch"
|
|
|
|
|
disabled={totalBranches <= 1}
|
|
|
|
|
onClick={goToPrevious}
|
|
|
|
|
size="icon-sm"
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children ?? <ChevronLeftIcon size={14} />}
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageBranchNextProps = ComponentProps<typeof Button>;
|
|
|
|
|
|
|
|
|
|
export const MessageBranchNext = ({
|
|
|
|
|
children,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageBranchNextProps) => {
|
|
|
|
|
const { goToNext, totalBranches } = useMessageBranch();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
aria-label="Next branch"
|
|
|
|
|
disabled={totalBranches <= 1}
|
|
|
|
|
onClick={goToNext}
|
|
|
|
|
size="icon-sm"
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children ?? <ChevronRightIcon size={14} />}
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageBranchPageProps = HTMLAttributes<HTMLSpanElement>;
|
|
|
|
|
|
|
|
|
|
export const MessageBranchPage = ({
|
|
|
|
|
className,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageBranchPageProps) => {
|
|
|
|
|
const { currentBranch, totalBranches } = useMessageBranch();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ButtonGroupText
|
|
|
|
|
className={cn(
|
|
|
|
|
"border-none bg-transparent text-muted-foreground shadow-none",
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{currentBranch + 1} of {totalBranches}
|
|
|
|
|
</ButtonGroupText>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type MessageResponseProps = ComponentProps<typeof Streamdown>;
|
|
|
|
|
|
|
|
|
|
// Type-only version skew: @streamdown/code bundles shiki@3 types while
|
|
|
|
|
// streamdown@2.5 expects shiki@4 types. Both work at runtime (each ships its
|
|
|
|
|
// own highlighter); the cast papers over the .d.ts mismatch.
|
2026-08-22 09:38:54 +03:00
|
|
|
//
|
|
|
|
|
// singleDollarTextMath: false — a bare $ is ALWAYS literal currency (the
|
|
|
|
|
// teacher-prompt contract). Inline math arrives as \(...\) and display math
|
|
|
|
|
// as \[...\]; transformMathDelimiters (below) rewrites both to the
|
|
|
|
|
// two-dollar forms remark-math still parses. $ can never start math.
|
|
|
|
|
//
|
|
|
|
|
// remark-breaks rides in the cjk plugin's remarkPluginsAfter slot (public
|
|
|
|
|
// PluginConfig API): Streamdown's bare `remarkPlugins` PROP would REPLACE the
|
|
|
|
|
// defaults ([remark-gfm, codeMeta]), silently dropping GFM and code-fence
|
|
|
|
|
// metastring parsing. The slot appends after the defaults, which is the
|
|
|
|
|
// standard remark-gfm → remark-breaks order.
|
|
|
|
|
//
|
2026-09-19 13:16:34 +03:00
|
|
|
// MERMAID is app-owned since the chrome-melt wave (2026-09-19): the md-blocks
|
|
|
|
|
// `code` override intercepts mermaid fences and renders via
|
|
|
|
|
// components/mermaid-render.ts — streamdown's internal mermaid machinery
|
|
|
|
|
// (the plugin entry + the `mermaid` prop) is retired with it.
|
2026-08-22 09:42:39 +03:00
|
|
|
export const streamdownPlugins = {
|
2026-08-22 09:38:54 +03:00
|
|
|
cjk: {
|
|
|
|
|
...cjk,
|
|
|
|
|
remarkPluginsAfter: [...cjk.remarkPluginsAfter, remarkBreaks],
|
|
|
|
|
},
|
2026-08-22 03:23:11 +03:00
|
|
|
code,
|
2026-08-22 09:38:54 +03:00
|
|
|
math: createMathPlugin({ singleDollarTextMath: false }),
|
2026-08-22 03:23:11 +03:00
|
|
|
} as unknown as PluginConfig;
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
export const MessageResponse = memo(
|
2026-08-22 09:38:54 +03:00
|
|
|
({ className, children, ...props }: MessageResponseProps) => {
|
|
|
|
|
// Delimiter preprocessing is a single linear scan (no markdown parsing)
|
|
|
|
|
// — cheap enough for the render path of this children-memoized
|
|
|
|
|
// component. Unbalanced delimiters pass through untouched, so mid-stream
|
|
|
|
|
// chunks render literally until the closing delimiter arrives and the
|
|
|
|
|
// full text is re-transformed.
|
|
|
|
|
const content =
|
|
|
|
|
typeof children === "string"
|
|
|
|
|
? transformMathDelimiters(children)
|
|
|
|
|
: children;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Streamdown
|
|
|
|
|
className={cn(
|
2026-09-12 23:45:44 +03:00
|
|
|
// Source Sans 3 + emphasis-color tokens on every markdown surface
|
|
|
|
|
// (labels stay JetBrains Mono). Unlayered plain CSS wins over text-sm.
|
|
|
|
|
"lh-prose size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0",
|
2026-08-22 09:38:54 +03:00
|
|
|
className
|
|
|
|
|
)}
|
2026-09-19 13:16:34 +03:00
|
|
|
components={mdComponents}
|
2026-08-22 09:38:54 +03:00
|
|
|
plugins={streamdownPlugins}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{content}
|
|
|
|
|
</Streamdown>
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-08-22 02:18:27 +03:00
|
|
|
(prevProps, nextProps) =>
|
|
|
|
|
prevProps.children === nextProps.children &&
|
|
|
|
|
nextProps.isAnimating === prevProps.isAnimating
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
MessageResponse.displayName = "MessageResponse";
|
|
|
|
|
|
|
|
|
|
export type MessageToolbarProps = ComponentProps<"div">;
|
|
|
|
|
|
|
|
|
|
export const MessageToolbar = ({
|
|
|
|
|
className,
|
|
|
|
|
children,
|
|
|
|
|
...props
|
|
|
|
|
}: MessageToolbarProps) => (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"mt-4 flex w-full items-center justify-between gap-4",
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|