| 73 | } |
| 74 | |
| 75 | function ToolDescription(props: { description: string }) { |
| 76 | const { description } = props; |
| 77 | const contentRef = useRef<HTMLDivElement>(null); |
| 78 | const [expanded, setExpanded] = useState(false); |
| 79 | const [canExpand, setCanExpand] = useState( |
| 80 | description.length > 160 || description.includes("\n"), |
| 81 | ); |
| 82 | |
| 83 | useEffect(() => { |
| 84 | setExpanded(false); |
| 85 | setCanExpand(description.length > 160 || description.includes("\n")); |
| 86 | }, [description]); |
| 87 | |
| 88 | useEffect(() => { |
| 89 | if (expanded) return; |
| 90 | |
| 91 | const element = contentRef.current; |
| 92 | if (!element) return; |
| 93 | |
| 94 | const measure = () => { |
| 95 | setCanExpand( |
| 96 | element.scrollHeight > element.clientHeight + 1 || |
| 97 | description.length > 160 || |
| 98 | description.includes("\n"), |
| 99 | ); |
| 100 | }; |
| 101 | |
| 102 | measure(); |
| 103 | const win = element.ownerDocument.defaultView; |
| 104 | win?.addEventListener("resize", measure); |
| 105 | return () => win?.removeEventListener("resize", measure); |
| 106 | }, [description, expanded]); |
| 107 | |
| 108 | return ( |
| 109 | <div className="mt-1.5 max-w-2xl"> |
| 110 | <div |
| 111 | ref={contentRef} |
| 112 | className={cn("text-sm text-muted-foreground", !expanded && "line-clamp-2")} |
| 113 | > |
| 114 | <Markdown>{description}</Markdown> |
| 115 | </div> |
| 116 | {canExpand && ( |
| 117 | <Button |
| 118 | type="button" |
| 119 | variant="ghost" |
| 120 | size="xs" |
| 121 | aria-expanded={expanded} |
| 122 | className="mt-1 h-6 px-1.5 text-xs text-muted-foreground hover:text-foreground" |
| 123 | onClick={() => setExpanded((current) => !current)} |
| 124 | > |
| 125 | <ChevronDownIcon |
| 126 | aria-hidden |
| 127 | className={cn("size-3 transition-transform", expanded && "rotate-180")} |
| 128 | /> |
| 129 | {expanded ? "Show less" : "Show more"} |
| 130 | </Button> |
| 131 | )} |
| 132 | </div> |