|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { atoms } from "@/app/store/global"; |
| 5 | +import { useAtomValue } from "jotai"; |
| 6 | +import { memo, useRef, useState } from "react"; |
| 7 | +import { WaveAIModel } from "./waveai-model"; |
| 8 | + |
| 9 | +type ThinkingMode = "quick" | "balanced" | "deep"; |
| 10 | + |
| 11 | +interface ThinkingModeMetadata { |
| 12 | + icon: string; |
| 13 | + name: string; |
| 14 | + desc: string; |
| 15 | + premium: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +const ThinkingModeData: Record<ThinkingMode, ThinkingModeMetadata> = { |
| 19 | + quick: { |
| 20 | + icon: "fa-bolt", |
| 21 | + name: "Quick", |
| 22 | + desc: "Fastest responses (gpt-5-mini)", |
| 23 | + premium: false, |
| 24 | + }, |
| 25 | + balanced: { |
| 26 | + icon: "fa-sparkles", |
| 27 | + name: "Balanced", |
| 28 | + desc: "Good mix of speed and accuracy\n(gpt-5 with minimal thinking)", |
| 29 | + premium: true, |
| 30 | + }, |
| 31 | + deep: { |
| 32 | + icon: "fa-lightbulb", |
| 33 | + name: "Deep", |
| 34 | + desc: "Slower but most capable\n(gpt-5 with full reasoning)", |
| 35 | + premium: true, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +export const ThinkingLevelDropdown = memo(() => { |
| 40 | + const model = WaveAIModel.getInstance(); |
| 41 | + const thinkingMode = useAtomValue(model.thinkingMode); |
| 42 | + const rateLimitInfo = useAtomValue(atoms.waveAIRateLimitInfoAtom); |
| 43 | + const [isOpen, setIsOpen] = useState(false); |
| 44 | + const dropdownRef = useRef<HTMLDivElement>(null); |
| 45 | + |
| 46 | + const hasPremium = !rateLimitInfo || rateLimitInfo.unknown || rateLimitInfo.preq > 0; |
| 47 | + |
| 48 | + const handleSelect = (mode: ThinkingMode) => { |
| 49 | + const metadata = ThinkingModeData[mode]; |
| 50 | + if (!hasPremium && metadata.premium) { |
| 51 | + return; |
| 52 | + } |
| 53 | + model.setThinkingMode(mode); |
| 54 | + setIsOpen(false); |
| 55 | + }; |
| 56 | + |
| 57 | + let currentMode = (thinkingMode as ThinkingMode) || "balanced"; |
| 58 | + const currentMetadata = ThinkingModeData[currentMode]; |
| 59 | + if (!hasPremium && currentMetadata.premium) { |
| 60 | + currentMode = "quick"; |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="relative" ref={dropdownRef}> |
| 65 | + <button |
| 66 | + onClick={() => setIsOpen(!isOpen)} |
| 67 | + className="flex items-center gap-1.5 px-2 py-1 text-xs text-gray-300 hover:text-white bg-gray-800/50 hover:bg-gray-700/50 rounded transition-colors cursor-pointer border border-gray-600/50" |
| 68 | + title={`Thinking: ${currentMetadata.name}`} |
| 69 | + > |
| 70 | + <i className={`fa ${currentMetadata.icon} text-[10px]`}></i> |
| 71 | + <span className="text-[11px]">{currentMetadata.name}</span> |
| 72 | + <i className="fa fa-chevron-down text-[8px]"></i> |
| 73 | + </button> |
| 74 | + |
| 75 | + {isOpen && ( |
| 76 | + <> |
| 77 | + <div className="fixed inset-0 z-40" onClick={() => setIsOpen(false)} /> |
| 78 | + <div className="absolute top-full right-0 mt-1 bg-gray-800 border border-gray-600 rounded shadow-lg z-50 min-w-[280px]"> |
| 79 | + {(Object.keys(ThinkingModeData) as ThinkingMode[]).map((mode, index) => { |
| 80 | + const metadata = ThinkingModeData[mode]; |
| 81 | + const isFirst = index === 0; |
| 82 | + const isLast = index === Object.keys(ThinkingModeData).length - 1; |
| 83 | + const isDisabled = !hasPremium && metadata.premium; |
| 84 | + const isSelected = currentMode === mode; |
| 85 | + return ( |
| 86 | + <button |
| 87 | + key={mode} |
| 88 | + onClick={() => handleSelect(mode)} |
| 89 | + disabled={isDisabled} |
| 90 | + className={`w-full flex flex-col gap-0.5 px-3 ${ |
| 91 | + isFirst ? "pt-1 pb-0.5" : isLast ? "pt-0.5 pb-1" : "pt-0.5 pb-0.5" |
| 92 | + } ${ |
| 93 | + isDisabled |
| 94 | + ? "text-gray-500 cursor-not-allowed" |
| 95 | + : "text-gray-300 hover:bg-gray-700 cursor-pointer" |
| 96 | + } transition-colors text-left`} |
| 97 | + > |
| 98 | + <div className="flex items-center gap-2 w-full"> |
| 99 | + <i className={`fa ${metadata.icon}`}></i> |
| 100 | + <span className={`text-sm ${isSelected ? "font-bold" : ""}`}> |
| 101 | + {metadata.name} |
| 102 | + {isDisabled && " (premium)"} |
| 103 | + </span> |
| 104 | + {isSelected && <i className="fa fa-check ml-auto"></i>} |
| 105 | + </div> |
| 106 | + <div className="text-xs text-muted pl-5" style={{ whiteSpace: "pre-line" }}> |
| 107 | + {metadata.desc} |
| 108 | + </div> |
| 109 | + </button> |
| 110 | + ); |
| 111 | + })} |
| 112 | + </div> |
| 113 | + </> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +ThinkingLevelDropdown.displayName = "ThinkingLevelDropdown"; |
0 commit comments