| 1 | import { |
| 2 | CubeIcon, |
| 3 | ExclamationTriangleIcon, |
| 4 | PencilIcon, |
| 5 | WrenchScrewdriverIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { useContext, useEffect, useState } from "react"; |
| 8 | import { useNavigate } from "react-router-dom"; |
| 9 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 10 | import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; |
| 11 | import { |
| 12 | selectPendingToolCalls, |
| 13 | selectToolCallsByStatus, |
| 14 | } from "../../../../redux/selectors/selectToolCalls"; |
| 15 | import { setSelectedProfile } from "../../../../redux/slices/profilesSlice"; |
| 16 | import FreeTrialButton from "../../../FreeTrialButton"; |
| 17 | import { ToolTip } from "../../../gui/Tooltip"; |
| 18 | import HoverItem from "../../InputToolbar/HoverItem"; |
| 19 | |
| 20 | import { usesFreeTrialApiKey } from "core/config/usesFreeTrialApiKey"; |
| 21 | import type { FreeTrialStatus } from "core/control-plane/client"; |
| 22 | import { useAuth } from "../../../../context/Auth"; |
| 23 | import { getLocalStorage } from "../../../../util/localStorage"; |
| 24 | import { CONFIG_ROUTES } from "../../../../util/navigation"; |
| 25 | import { AssistantAndOrgListbox } from "../../../AssistantAndOrgListbox"; |
| 26 | |
| 27 | export function BlockSettingsTopToolbar() { |
| 28 | const navigate = useNavigate(); |
| 29 | const dispatch = useAppDispatch(); |
| 30 | const { selectedProfile } = useAuth(); |
| 31 | |
| 32 | const configError = useAppSelector((store) => store.config.configError); |
| 33 | const config = useAppSelector((state) => state.config.config); |
| 34 | const ideMessenger = useContext(IdeMessengerContext); |
| 35 | |
| 36 | const pendingToolCalls = useAppSelector(selectPendingToolCalls); |
| 37 | const callingToolCalls = useAppSelector((state) => |
| 38 | selectToolCallsByStatus(state, "calling"), |
| 39 | ); |
| 40 | const hasActiveContent = |
| 41 | pendingToolCalls.length > 0 || callingToolCalls.length > 0; |
| 42 | |
| 43 | const [freeTrialStatus, setFreeTrialStatus] = |
| 44 | useState<FreeTrialStatus | null>(null); |
| 45 | const hasExitedFreeTrial = getLocalStorage("hasExitedFreeTrial"); |
| 46 | const isUsingFreeTrial = usesFreeTrialApiKey(config) && !hasExitedFreeTrial; |
| 47 | |
| 48 | useEffect(() => { |
| 49 | const fetchFreeTrialStatus = () => { |
| 50 | ideMessenger |
| 51 | .request("controlPlane/getFreeTrialStatus", undefined) |
| 52 | .then((resp) => { |
| 53 | if (resp.status === "success") { |
| 54 | setFreeTrialStatus(resp.content); |
| 55 | } |
| 56 | }) |
| 57 | .catch(() => {}); |
| 58 | }; |
| 59 | |
| 60 | fetchFreeTrialStatus(); |
| 61 | |
| 62 | let intervalId: NodeJS.Timeout | null = null; |
| 63 | |
| 64 | if (isUsingFreeTrial) { |
| 65 | intervalId = setInterval(fetchFreeTrialStatus, 15000); |
| 66 | } |
| 67 | |
| 68 | return () => { |
| 69 | if (intervalId) { |
| 70 | clearInterval(intervalId); |
| 71 | } |
| 72 | }; |
| 73 | }, [ideMessenger, isUsingFreeTrial]); |
| 74 | |
| 75 | const shouldShowError = configError && configError?.length > 0; |
| 76 | |
| 77 | const handleRulesClick = () => { |
| 78 | if (selectedProfile) { |
| 79 | dispatch(setSelectedProfile(selectedProfile.id)); |
| 80 | ideMessenger.post("didChangeSelectedProfile", { |
| 81 | id: selectedProfile.id, |
| 82 | }); |
| 83 | } |
| 84 | navigate(CONFIG_ROUTES.RULES); |
| 85 | }; |
| 86 | |
| 87 | const handleToolsClick = () => { |
| 88 | if (selectedProfile) { |
| 89 | dispatch(setSelectedProfile(selectedProfile.id)); |
| 90 | ideMessenger.post("didChangeSelectedProfile", { |
| 91 | id: selectedProfile.id, |
| 92 | }); |
| 93 | } |
| 94 | navigate(CONFIG_ROUTES.TOOLS); |
| 95 | }; |
| 96 | |
| 97 | const handleModelsClick = () => { |
| 98 | if (selectedProfile) { |
| 99 | dispatch(setSelectedProfile(selectedProfile.id)); |
| 100 | ideMessenger.post("didChangeSelectedProfile", { |
| 101 | id: selectedProfile.id, |
| 102 | }); |
| 103 | } |
| 104 | navigate(CONFIG_ROUTES.MODELS); |
| 105 | }; |
| 106 | |
| 107 | return ( |
| 108 | <div className="flex flex-1 items-center justify-between gap-3"> |
| 109 | <div className="flex items-center gap-1"> |
| 110 | {shouldShowError && ( |
| 111 | <ToolTip delayShow={700} content="View configuration errors"> |
| 112 | <div |
| 113 | role="button" |
| 114 | tabIndex={0} |
| 115 | onClick={() => navigate(CONFIG_ROUTES.AGENTS)} |
| 116 | onKeyDown={(e) => { |
| 117 | if (e.key === "Enter" || e.key === " ") { |
| 118 | e.preventDefault(); |
| 119 | navigate(CONFIG_ROUTES.AGENTS); |
| 120 | } |
| 121 | }} |
| 122 | data-testid="block-settings-toolbar-icon-error" |
| 123 | className="relative flex cursor-pointer select-none items-center rounded-full px-1.5 py-1 sm:px-1.5" |
| 124 | > |
| 125 | <ExclamationTriangleIcon |
| 126 | className="text-warning h-[13px] w-[13px] flex-shrink-0" |
| 127 | aria-hidden="true" |
| 128 | /> |
| 129 | </div> |
| 130 | </ToolTip> |
| 131 | )} |
| 132 | |
| 133 | {!hasActiveContent && ( |
| 134 | <div className="flex items-center gap-1.5"> |
| 135 | <ToolTip content="Configure rules"> |
| 136 | <HoverItem onClick={handleRulesClick} px={2}> |
| 137 | <PencilIcon className="text-description-muted h-3 w-3 hover:brightness-125" /> |
| 138 | </HoverItem> |
| 139 | </ToolTip> |
| 140 | |
| 141 | <ToolTip content="Configure tools"> |
| 142 | <HoverItem onClick={handleToolsClick} px={2}> |
| 143 | <WrenchScrewdriverIcon className="text-description-muted h-3 w-3 hover:brightness-125" /> |
| 144 | </HoverItem> |
| 145 | </ToolTip> |
| 146 | |
| 147 | <ToolTip content="Configure models"> |
| 148 | <HoverItem onClick={handleModelsClick} px={2}> |
| 149 | <CubeIcon className="text-description-muted h-3 w-3 hover:brightness-125" /> |
| 150 | </HoverItem> |
| 151 | </ToolTip> |
| 152 | </div> |
| 153 | )} |
| 154 | </div> |
| 155 | |
| 156 | <ToolTip |
| 157 | place="top" |
| 158 | content={isUsingFreeTrial ? "View free trial usage" : "Select Agent"} |
| 159 | > |
| 160 | <div> |
| 161 | {isUsingFreeTrial ? ( |
| 162 | <FreeTrialButton freeTrialStatus={freeTrialStatus} /> |
| 163 | ) : ( |
| 164 | <AssistantAndOrgListbox variant="lump" /> |
| 165 | )} |
| 166 | </div> |
| 167 | </ToolTip> |
| 168 | </div> |
| 169 | ); |
| 170 | } |