// ============================================================================ // Tool Response Card // ============================================================================ import { Card, CardBody, CardBodyProps, CardProps, CardTitle, CardTitleProps, Divider, DividerProps, ExpandableSection, ExpandableSectionProps } from '@patternfly/react-core'; import { useState, type FunctionComponent } from 'react'; import MarkdownContent from '../MarkdownContent'; import type { MarkdownContentProps } from '../MarkdownContent'; export interface ToolResponseProps { /** Toggle content shown for expandable section */ toggleContent: React.ReactNode; /** Flag indicating whether the expandable content is expanded by default. */ isDefaultExpanded?: boolean; /** Additional props passed to expandable section */ expandableSectionProps?: Omit; /** Subheading rendered inside expandable section */ subheading?: string; /** Body text rendered inside expandable section */ body?: React.ReactNode | string; /** Content passed into tool response card body */ cardBody?: React.ReactNode; /** Content passed into tool response card title */ cardTitle?: React.ReactNode; /** Additional props passed to main card */ cardProps?: CardProps; /** Additional props passed to main card body */ cardBodyProps?: CardBodyProps; /** Additional props passed to tool response card */ toolResponseCardProps?: CardProps; /** Additional props passed to tool response card body */ toolResponseCardBodyProps?: CardBodyProps; /** Additional props passed to tool response card divider */ toolResponseCardDividerProps?: DividerProps; /** Additional props passed to tool response card title */ toolResponseCardTitleProps?: CardTitleProps; /** Whether to enable markdown rendering for toggleContent. When true and toggleContent is a string, it will be parsed as markdown. */ isToggleContentMarkdown?: boolean; /** Whether to enable markdown rendering for subheading. When true, subheading will be parsed as markdown. */ isSubheadingMarkdown?: boolean; /** Whether to enable markdown rendering for body. When true and body is a string, it will be parsed as markdown. */ isBodyMarkdown?: boolean; /** Whether to enable markdown rendering for cardBody. When true and cardBody is a string, it will be parsed as markdown. */ isCardBodyMarkdown?: boolean; /** Whether to enable markdown rendering for cardTitle. When true and cardTitle is a string, it will be parsed as markdown. */ isCardTitleMarkdown?: boolean; /** Props passed to MarkdownContent component when markdown is enabled */ markdownContentProps?: Omit; /** Whether to retain styles in the MarkdownContent component. Defaults to false. */ shouldRetainStyles?: boolean; } export const ToolResponse: FunctionComponent = ({ body, cardProps, expandableSectionProps, subheading, cardBody, cardTitle, cardBodyProps, toggleContent, isDefaultExpanded = true, toolResponseCardBodyProps, toolResponseCardDividerProps, toolResponseCardProps, toolResponseCardTitleProps, isToggleContentMarkdown, isSubheadingMarkdown, isBodyMarkdown, isCardBodyMarkdown, isCardTitleMarkdown, markdownContentProps, shouldRetainStyles = false }: ToolResponseProps) => { const [isExpanded, setIsExpanded] = useState(isDefaultExpanded); const onToggle = (_event: React.MouseEvent, isExpanded: boolean) => { setIsExpanded(isExpanded); }; const renderToggleContent = () => { if (isToggleContentMarkdown && typeof toggleContent === 'string') { return ( ); } return toggleContent; }; const renderSubheading = () => { if (!subheading) { return null; } if (isSubheadingMarkdown) { return ; } return subheading; }; const renderBody = () => { if (!body) { return null; } if (isBodyMarkdown && typeof body === 'string') { return ; } return body; }; const renderCardTitle = () => { if (!cardTitle) { return null; } if (isCardTitleMarkdown && typeof cardTitle === 'string') { return ; } return cardTitle; }; const renderCardBody = () => { if (!cardBody) { return null; } if (isCardBodyMarkdown && typeof cardBody === 'string') { return ; } return cardBody; }; return (
{subheading && (
{renderSubheading()}
)} {body &&
{renderBody()}
} {(cardTitle || cardBody) && ( {cardTitle && {renderCardTitle()}} {cardTitle && cardBody && } {cardBody && {renderCardBody()}} )}
); }; export default ToolResponse;