import { HoverCard, HoverCardContent, HoverCardTrigger, } from '@/components/ui/hover-card'; import { cn } from '@/lib/utils'; import { get, isEmpty, isPlainObject } from 'lodash'; import { ChevronRight } from 'lucide-react'; import { PropsWithChildren, ReactNode, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { JsonSchemaDataType } from '../../constant'; import { useGetStructuredOutputByValue } from '../../hooks/use-build-structured-output'; import { hasJsonSchemaChild, hasSpecificTypeChild, } from '../../utils/filter-agent-structured-output'; type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode }; type StructuredOutputSecondaryMenuProps = { data: DataItem; click(option: { label: ReactNode; value: string }): void; types?: JsonSchemaDataType[]; } & PropsWithChildren; export function StructuredOutputSecondaryMenu({ data, click, types = [], }: StructuredOutputSecondaryMenuProps) { const { t } = useTranslation(); const filterStructuredOutput = useGetStructuredOutputByValue(); const structuredOutput = filterStructuredOutput(data.value); const handleSubMenuClick = useCallback( (option: { label: ReactNode; value: string }, dataType?: string) => () => { // The query variable of the iteration operator can only select array type data. if ( (!isEmpty(types) && types?.some((x) => x === dataType)) || isEmpty(types) ) { click(option); } }, [click, types], ); const handleMenuClick = useCallback(() => { if (isEmpty(types) || types?.some((x) => x === JsonSchemaDataType.Object)) { click(data); } }, [click, data, types]); const renderAgentStructuredOutput = useCallback( (values: any, option: { label: ReactNode; value: string }) => { const properties = get(values, 'properties') || get(values, 'items.properties'); if (isPlainObject(values) && properties) { return ( ); } return
; }, [handleSubMenuClick, types], ); if ( !hasJsonSchemaChild(structuredOutput) || (!isEmpty(types) && !hasSpecificTypeChild(structuredOutput, types)) ) { return null; } return (
  • {data.label}
  • {t('flow.structuredOutput.structuredOutput')}
    {renderAgentStructuredOutput(structuredOutput, data)}
    ); }