| 1 | import { |
| 2 | ChatBubbleLeftIcon, |
| 3 | ChevronLeftIcon, |
| 4 | CubeIcon, |
| 5 | EllipsisHorizontalIcon, |
| 6 | ExclamationTriangleIcon, |
| 7 | PencilIcon, |
| 8 | Squares2X2Icon, |
| 9 | WrenchScrewdriverIcon, |
| 10 | } from "@heroicons/react/24/outline"; |
| 11 | import { ReactNode, useContext, useEffect, useState } from "react"; |
| 12 | import { vscBadgeForeground } from "../../.."; |
| 13 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 14 | import { useAppSelector } from "../../../../redux/hooks"; |
| 15 | import FreeTrialButton from "../../../FreeTrialButton"; |
| 16 | import { ToolTip } from "../../../gui/Tooltip"; |
| 17 | import { useFontSize } from "../../../ui/font"; |
| 18 | import HoverItem from "../../InputToolbar/HoverItem"; |
| 19 | import { useLump } from "../LumpContext"; |
| 20 | import { ErrorsSectionTooltip } from "../sections/errors/ErrorsSectionTooltip"; |
| 21 | import { McpSectionTooltip } from "../sections/mcp/MCPTooltip"; |
| 22 | import { ToolsSectionTooltip } from "../sections/tool-policies/ToolPoliciesSectionTooltip"; |
| 23 | |
| 24 | import { usesFreeTrialApiKey } from "core/config/usesFreeTrialApiKey"; |
| 25 | import type { FreeTrialStatus } from "core/control-plane/client"; |
| 26 | import { getLocalStorage } from "../../../../util/localStorage"; |
| 27 | import { AssistantAndOrgListbox } from "../../../AssistantAndOrgListbox"; |
| 28 | |
| 29 | interface BlockSettingsToolbarIcon { |
| 30 | title: string; |
| 31 | tooltip: ReactNode; |
| 32 | icon: React.ComponentType<any>; |
| 33 | itemCount?: number; |
| 34 | onClick: () => void; |
| 35 | isSelected?: boolean; |
| 36 | className?: string; |
| 37 | } |
| 38 | |
| 39 | interface Section { |
| 40 | id: string; |
| 41 | title: string; |
| 42 | tooltip: ReactNode; |
| 43 | icon: React.ComponentType<any>; |
| 44 | } |
| 45 | |
| 46 | const sections: Section[] = [ |
| 47 | { id: "models", title: "Models", tooltip: "Models", icon: CubeIcon }, |
| 48 | { id: "rules", title: "Rules", tooltip: "Rules", icon: PencilIcon }, |
| 49 | { |
| 50 | id: "prompts", |
| 51 | title: "Prompts", |
| 52 | tooltip: "Prompts", |
| 53 | icon: ChatBubbleLeftIcon, |
| 54 | }, |
| 55 | { |
| 56 | id: "tools", |
| 57 | title: "Tools", |
| 58 | tooltip: <ToolsSectionTooltip />, |
| 59 | icon: WrenchScrewdriverIcon, |
| 60 | }, |
| 61 | { |
| 62 | id: "mcp", |
| 63 | title: "MCP", |
| 64 | tooltip: <McpSectionTooltip />, |
| 65 | icon: Squares2X2Icon, |
| 66 | }, |
| 67 | { |
| 68 | id: "error", |
| 69 | title: "Errors", |
| 70 | tooltip: <ErrorsSectionTooltip />, |
| 71 | icon: ExclamationTriangleIcon, |
| 72 | }, |
| 73 | ]; |
| 74 | |
| 75 | function BlockSettingsToolbarIcon( |
| 76 | props: BlockSettingsToolbarIcon & { sectionId?: string }, |
| 77 | ) { |
| 78 | const isErrorSection = props.sectionId === "error"; |
| 79 | |
| 80 | const id = `block-settings-toolbar-icon-${props.sectionId}`; |
| 81 | |
| 82 | const fontSize = useFontSize(-3); |
| 83 | return ( |
| 84 | <> |
| 85 | <HoverItem |
| 86 | px={0} |
| 87 | onClick={props.onClick} |
| 88 | data-testid={id} |
| 89 | data-tooltip-id={id} |
| 90 | > |
| 91 | <div |
| 92 | role="button" |
| 93 | tabIndex={0} |
| 94 | onKeyDown={(e) => { |
| 95 | if (e.key === "Enter" || e.key === " ") { |
| 96 | e.preventDefault(); |
| 97 | props.onClick(); |
| 98 | } |
| 99 | }} |
| 100 | className={`${ |
| 101 | props.isSelected |
| 102 | ? isErrorSection |
| 103 | ? "bg-error" |
| 104 | : "bg-badge" |
| 105 | : undefined |
| 106 | } relative flex select-none items-center rounded-full px-[3px] py-0.5 transition-all duration-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500/50 sm:px-1 ${props.className || ""}`} |
| 107 | > |
| 108 | <props.icon |
| 109 | className={`h-[13px] w-[13px] flex-shrink-0 hover:brightness-125 ${ |
| 110 | isErrorSection ? "text-error" : "" |
| 111 | }`} |
| 112 | style={{ |
| 113 | color: props.isSelected ? vscBadgeForeground : undefined, |
| 114 | }} |
| 115 | aria-hidden="true" |
| 116 | /> |
| 117 | <div |
| 118 | style={{ fontSize }} |
| 119 | className={`overflow-hidden transition-all duration-200 ${ |
| 120 | props.isSelected ? "ml-1 w-auto opacity-100" : "w-0 opacity-0" |
| 121 | }`} |
| 122 | > |
| 123 | <span |
| 124 | className="whitespace-nowrap" |
| 125 | style={{ color: vscBadgeForeground }} |
| 126 | > |
| 127 | {props.title} |
| 128 | </span> |
| 129 | </div> |
| 130 | </div> |
| 131 | </HoverItem> |
| 132 | <ToolTip delayShow={700} id={id}> |
| 133 | {props.tooltip} |
| 134 | </ToolTip> |
| 135 | </> |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | export function BlockSettingsTopToolbar() { |
| 140 | const { |
| 141 | isToolbarExpanded, |
| 142 | toggleToolbar, |
| 143 | selectedSection, |
| 144 | setSelectedSection, |
| 145 | } = useLump(); |
| 146 | |
| 147 | const configError = useAppSelector((store) => store.config.configError); |
| 148 | const config = useAppSelector((state) => state.config.config); |
| 149 | const ideMessenger = useContext(IdeMessengerContext); |
| 150 | |
| 151 | const [freeTrialStatus, setFreeTrialStatus] = |
| 152 | useState<FreeTrialStatus | null>(null); |
| 153 | const hasExitedFreeTrial = getLocalStorage("hasExitedFreeTrial"); |
| 154 | const isUsingFreeTrial = usesFreeTrialApiKey(config) && !hasExitedFreeTrial; |
| 155 | |
| 156 | useEffect(() => { |
| 157 | const fetchFreeTrialStatus = () => { |
| 158 | ideMessenger |
| 159 | .request("controlPlane/getFreeTrialStatus", undefined) |
| 160 | .then((resp) => { |
| 161 | if (resp.status === "success") { |
| 162 | setFreeTrialStatus(resp.content); |
| 163 | } |
| 164 | }) |
| 165 | .catch(() => {}); |
| 166 | }; |
| 167 | |
| 168 | fetchFreeTrialStatus(); |
| 169 | |
| 170 | let intervalId: NodeJS.Timeout | null = null; |
| 171 | |
| 172 | if (isUsingFreeTrial) { |
| 173 | intervalId = setInterval(fetchFreeTrialStatus, 15000); |
| 174 | } |
| 175 | |
| 176 | return () => { |
| 177 | if (intervalId) { |
| 178 | clearInterval(intervalId); |
| 179 | } |
| 180 | }; |
| 181 | }, [ideMessenger, isUsingFreeTrial]); |
| 182 | |
| 183 | const handleEllipsisClick = () => { |
| 184 | if (isToolbarExpanded) { |
| 185 | setSelectedSection(null); |
| 186 | } |
| 187 | toggleToolbar(); |
| 188 | }; |
| 189 | |
| 190 | const visibleSections = sections.filter( |
| 191 | (section) => |
| 192 | section.id !== "error" || |
| 193 | (section.id === "error" && configError && configError?.length > 0), |
| 194 | ); |
| 195 | |
| 196 | return ( |
| 197 | <div className="flex flex-1 items-center justify-between gap-2"> |
| 198 | <div className="flex flex-row"> |
| 199 | <div className="xs:flex text-description hidden items-center justify-center gap-0.5"> |
| 200 | <BlockSettingsToolbarIcon |
| 201 | className="-ml-1.5" |
| 202 | icon={isToolbarExpanded ? ChevronLeftIcon : EllipsisHorizontalIcon} |
| 203 | tooltip={isToolbarExpanded ? "Collapse Toolbar" : "Expand Toolbar"} |
| 204 | title="" |
| 205 | isSelected={false} |
| 206 | onClick={handleEllipsisClick} |
| 207 | /> |
| 208 | <div |
| 209 | className={`${isToolbarExpanded ? "w-min" : "w-0"} flex overflow-hidden transition-all duration-200`} |
| 210 | > |
| 211 | <div className="flex"> |
| 212 | {visibleSections.map((section) => ( |
| 213 | <BlockSettingsToolbarIcon |
| 214 | key={section.id} |
| 215 | sectionId={section.id} |
| 216 | icon={section.icon} |
| 217 | tooltip={section.tooltip} |
| 218 | title={section.title} |
| 219 | isSelected={selectedSection === section.id} |
| 220 | onClick={() => |
| 221 | setSelectedSection( |
| 222 | selectedSection === section.id ? null : section.id, |
| 223 | ) |
| 224 | } |
| 225 | /> |
| 226 | ))} |
| 227 | </div> |
| 228 | </div> |
| 229 | </div> |
| 230 | </div> |
| 231 | <div className="flex gap-0.5"> |
| 232 | <HoverItem |
| 233 | data-tooltip-id="assistant-select-tooltip" |
| 234 | className="!m-0 !p-0" |
| 235 | > |
| 236 | {isUsingFreeTrial ? ( |
| 237 | <FreeTrialButton freeTrialStatus={freeTrialStatus} /> |
| 238 | ) : ( |
| 239 | <AssistantAndOrgListbox /> |
| 240 | )} |
| 241 | <ToolTip id="assistant-select-tooltip" place="top"> |
| 242 | {isUsingFreeTrial ? "View free trial usage" : "Select Agent"} |
| 243 | </ToolTip> |
| 244 | </HoverItem> |
| 245 | </div> |
| 246 | </div> |
| 247 | ); |
| 248 | } |