diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx index c025aceeff9..94be44c022d 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx @@ -298,22 +298,22 @@ export const Composer = memo(function Composer({ status={onCancelTurn ? "streaming" : "ready"} submitWhileStreaming={promptQueue !== undefined} toolbar={ -
- {configOptions && onSetConfigOption ? ( - - ) : null} - {modes && onSetMode ? ( - - ) : null} -
+ modes && onSetMode ? ( + + ) : undefined + } + toolbarEnd={ + configOptions && onSetConfigOption ? ( + + ) : undefined } /> diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModePicker/ModePicker.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModePicker/ModePicker.tsx index 649a6ad34a5..934857fc5d8 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModePicker/ModePicker.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModePicker/ModePicker.tsx @@ -15,15 +15,15 @@ import { Play, SlidersHorizontal, } from "lucide-react"; -import { useState } from "react"; import { LuCheck, LuChevronDown } from "react-icons/lu"; import { isUnrestrictedMode } from "../../../../utils/isUnrestrictedMode"; import { MENU_DESCRIPTION_CLASS, - MENU_ROW_CLASS, + MENU_PANEL_CLASS, PILL_CHEVRON_CLASS, PILL_TRIGGER_CLASS, } from "../../constants"; +import { useOptimisticSelections } from "../../hooks/useOptimisticSelections"; export type SessionMode = { id: string; label: string }; @@ -36,7 +36,7 @@ type ModeCopy = { const HIDDEN_MODE_IDS = new Set(["plan", "acceptEdits", "workspace-write"]); const SOLID_ICONS = new Set([Pause, Play, FastForward]); -const UNRESTRICTED_TINT = "text-amber-500 opacity-65"; +const UNRESTRICTED_TINT = "text-highlight"; export function ModePicker({ currentModeId, @@ -48,7 +48,8 @@ export function ModePicker({ onSelect: (modeId: string) => void; }) { const { t } = useLingui(); - const [highlightedId, setHighlightedId] = useState(); + const { shown, select } = useOptimisticSelections({ mode: currentModeId }); + const shownModeId = shown("mode"); const offeredModes = modes.filter((mode) => !HIDDEN_MODE_IDS.has(mode.id)); if (offeredModes.length < 2) return null; @@ -111,35 +112,33 @@ export function ModePicker({ }); const current = - modes.find((mode) => mode.id === currentModeId) ?? offeredModes[0]; + modes.find((mode) => mode.id === shownModeId) ?? offeredModes[0]; if (!current) return null; const currentCopy = copyFor(current); const CurrentIcon = currentCopy.icon; - const iconProps = (copy: ModeCopy) => ({ + const iconProps = (copy: ModeCopy, className: string) => ({ className: cn( - "size-3.5 shrink-0", + "shrink-0 text-current", + className, SOLID_ICONS.has(copy.icon) && "fill-current", - copy.unrestricted && UNRESTRICTED_TINT, ), strokeWidth: SOLID_ICONS.has(copy.icon) ? 1 : 2, }); - const highlighted = - offeredModes.find((mode) => mode.id === highlightedId) ?? current; - const footer = copyFor(highlighted).description; return ( - { - if (!open) setHighlightedId(undefined); - }} - > + @@ -149,41 +148,54 @@ export function ModePicker({ aria-label={t({ message: "How should the agent's actions be approved?", })} - className="w-[210px] rounded-xl" + className={cn(MENU_PANEL_CLASS, "w-[300px] p-1.5")} side="top" > {offeredModes.map((mode) => { const copy = copyFor(mode); const Icon = copy.icon; + const selected = mode.id === current.id; return ( setHighlightedId(mode.id)} onSelect={() => { - if (mode.id !== currentModeId) onSelect(mode.id); + if (mode.id === shownModeId) return; + select("mode", mode.id, () => onSelect(mode.id)); }} > - - {copy.title} - {mode.id === current.id ? ( - - ) : null} + + + + + {copy.title} + {copy.description ? ( + + {copy.description} + + ) : null} + + + {selected ? ( + + ) : null} + ); })} - {footer ? ( -
-
- {footer} -
-
- ) : null}
); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/ModelPicker.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/ModelPicker.tsx index 1dacb3614df..867c35b15da 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/ModelPicker.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/ModelPicker.tsx @@ -1,27 +1,31 @@ -import { Trans } from "@lingui/react/macro"; +import { useLingui } from "@lingui/react/macro"; import type { SessionConfigOption } from "@superset/chat/protocol"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, + DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@superset/ui/dropdown-menu"; import { cn } from "@superset/ui/utils"; -import { type KeyboardEvent, useRef, useState } from "react"; +import { type KeyboardEvent, useMemo, useRef, useState } from "react"; import { LuCheck, LuChevronDown, LuZap } from "react-icons/lu"; import { getPresetIcon, useIsDarkTheme, } from "renderer/assets/app-icons/preset-icons"; import { + MENU_PANEL_CLASS, MENU_ROW_CLASS, PILL_CHEVRON_CLASS, PILL_TRIGGER_CLASS, } from "../../constants"; -import { ModelFlyout } from "./components/ModelFlyout"; +import { useOptimisticSelections } from "../../hooks/useOptimisticSelections"; +import { EffortSliderCard } from "./components/EffortSliderCard"; +import { ModelPanel } from "./components/ModelPanel"; import type { AgentSwitcher } from "./types"; export type ModelPickerProps = { @@ -30,7 +34,10 @@ export type ModelPickerProps = { agentSwitcher?: AgentSwitcher; }; -const SUB_TRIGGER_CLASS = cn(MENU_ROW_CLASS, "h-9 text-[13px]"); +const TRAIT_ROW_CLASS = cn( + MENU_ROW_CLASS, + "[&>svg:last-child]:size-3 [&>svg:last-child]:text-muted-foreground/70", +); function isToggle(option: SessionConfigOption) { return ( @@ -50,31 +57,56 @@ export function ModelPicker({ configOptions, onSelect, }: ModelPickerProps) { + const { t } = useLingui(); const [open, setOpen] = useState(false); const searchRef = useRef(null); + const settingsRef = useRef(null); const isDark = useIsDarkTheme(); + const settled = useMemo( + () => + Object.fromEntries( + configOptions.map((option) => [option.id, option.currentValue]), + ), + [configOptions], + ); + const { shown, select } = useOptimisticSelections(settled); + const options = useMemo( + () => + configOptions.map((option) => { + const value = shown(option.id) ?? option.currentValue; + return value === option.currentValue + ? option + : { ...option, currentValue: value }; + }), + [configOptions, shown], + ); + const choose = (configId: string, value: string) => + select(configId, value, () => onSelect(configId, value)); const agentIcon = agentSwitcher ? getPresetIcon(agentSwitcher.currentPresetId, isDark) : undefined; - const model = configOptions.find( + const model = options.find( (option) => option.category === "model" && option.options.length > 0, ); const canSwitchAgent = (agentSwitcher?.agents.length ?? 0) > 1; - const settings = configOptions.filter( + const settings = options.filter( (option) => option.category !== "model" && option.category !== "mode" && option.options.length > 0, ); - const toggles = settings.filter(isToggle); - const selects = settings.filter((option) => !isToggle(option)); - const fastOn = toggles.some( - (option) => option.id === "fast" && option.currentValue === "on", + const fast = settings.find( + (option) => option.id === "fast" && isToggle(option), ); - const effortLabel = currentLabel( - settings.find((option) => option.category === "thought_level"), + const fastOn = fast?.currentValue === "on"; + const effort = settings.find((option) => option.category === "thought_level"); + const effortLabel = currentLabel(effort); + // The slider card owns effort and the fast toggle; the rest stay as rows. + const rows = settings.filter( + (option) => option !== effort && (effort ? option !== fast : true), ); if (!model && settings.length === 0 && !canSwitchAgent) return null; + const showsModels = Boolean(model) || canSwitchAgent; const currentAgentLabel = agentSwitcher?.agents.find( (agent) => agent.presetId === agentSwitcher.currentPresetId, )?.label; @@ -84,9 +116,38 @@ export function ModelPicker({ currentAgentLabel ?? settings[0]?.label; const pick = (option: SessionConfigOption, value: string) => { - if (value !== option.currentValue) onSelect(option.id, value); + if (value !== option.currentValue) choose(option.id, value); setOpen(false); }; + // Radix traps Tab inside the menu, so the settings row below the model + // panel is only reachable by hand: Tab past the last agent tab lands on + // its first control, Tab past its last control returns to the search. + const settingsControls = () => + Array.from( + settingsRef.current?.querySelectorAll( + 'button:not([tabindex="-1"]), [data-slot="slider-thumb"], [role="menuitem"]', + ) ?? [], + ); + const leavePanel = (direction: 1 | -1) => { + const controls = settingsControls(); + (direction === 1 ? controls[0] : controls.at(-1))?.focus(); + }; + const onSettingsKeyDown = (event: KeyboardEvent) => { + if (event.key !== "Tab") return; + event.preventDefault(); + event.stopPropagation(); + const controls = settingsControls(); + const next = + controls.indexOf(document.activeElement as HTMLElement) + + (event.shiftKey ? -1 : 1); + if (next >= 0 && next < controls.length) { + controls[next]?.focus(); + } else if (searchRef.current) { + searchRef.current.focus(); + } else { + controls.at(next < 0 ? -1 : 0)?.focus(); + } + }; // Hovering a row moves menu focus to it; typing must still reach the search. const sendTypingToSearch = (event: KeyboardEvent) => { const search = searchRef.current; @@ -98,133 +159,128 @@ export function ModelPicker({ }; return ( - - - - - - {toggles.map((option) => { - const on = option.currentValue === "on"; - return ( - { - event.preventDefault(); - onSelect(option.id, on ? "off" : "on"); + ) : null} + + + + + + {showsModels ? ( + { + setOpen(false); + agentSwitcher.onSwitch(presetId, picked); + }, + } + } + model={model} + onLeave={settings.length > 0 ? leavePanel : undefined} + onPick={(modelId) => { + if (model) pick(model, modelId); }} - role="menuitemcheckbox" + searchRef={searchRef} + /> + ) : null} + {settings.length > 0 ? ( +
- {option.label} -
+ ) : null} +
+
+ ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/EffortSliderCard/EffortSliderCard.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/EffortSliderCard/EffortSliderCard.tsx new file mode 100644 index 00000000000..e7697c36a5b --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/EffortSliderCard/EffortSliderCard.tsx @@ -0,0 +1,119 @@ +import { useLingui } from "@lingui/react/macro"; +import type { SessionConfigOption } from "@superset/chat/protocol"; +import { Slider } from "@superset/ui/slider"; +import { cn } from "@superset/ui/utils"; +import { useState } from "react"; +import { LuZap } from "react-icons/lu"; + +const SNAP_MOTION_CLASS = + "transition-[left,right,width] duration-150 ease-[cubic-bezier(0.22,1.1,0.36,1)] motion-reduce:transition-none"; + +const FAST_PILL_CLASS = + "flex h-6 shrink-0 items-center gap-1 rounded-md px-1.5 text-[11px] font-medium"; + +export function EffortSliderCard({ + effort, + fast, + onSelect, +}: { + effort: SessionConfigOption; + fast: SessionConfigOption | undefined; + onSelect: (configId: string, value: string) => void; +}) { + const { t } = useLingui(); + const levels = effort.options; + const lastIndex = Math.max(levels.length - 1, 0); + const settledIndex = Math.max( + levels.findIndex((level) => level.id === effort.currentValue), + 0, + ); + // The thumb follows the pointer; the agent hears about it once, on release. + const [dragIndex, setDragIndex] = useState(null); + const index = dragIndex ?? settledIndex; + const fastOn = fast?.currentValue === "on"; + const fastLabel = t({ message: "Fast" }); + const marks = levels.map((_, i) => + lastIndex === 0 ? 0 : (i / lastIndex) * 100, + ); + + return ( +
+
+ {fast ? ( + + ) : ( +
+
+ setDragIndex(next ?? null)} + onValueCommit={([next]) => { + setDragIndex(null); + const level = next === undefined ? undefined : levels[next]; + if (level && level.id !== effort.currentValue) { + onSelect(effort.id, level.id); + } + }} + rangeClassName={cn( + "rounded-full bg-highlight", + SNAP_MOTION_CLASS, + index === 0 && "opacity-0", + )} + step={1} + thumbClassName={cn( + "size-7 cursor-grab border-0 bg-white shadow-[0_1px_2px_rgba(0,0,0,0.12),0_0_0_0.5px_rgba(0,0,0,0.05)] hover:ring-0 focus-visible:ring-2 focus-visible:ring-ring/40 active:cursor-grabbing", + SNAP_MOTION_CLASS, + )} + trackClassName="overflow-visible bg-foreground/[0.14] data-[orientation=horizontal]:h-6" + value={[index]} + > + +
+
+ ); +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/EffortSliderCard/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/EffortSliderCard/index.ts new file mode 100644 index 00000000000..dd7f8ddd868 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/EffortSliderCard/index.ts @@ -0,0 +1 @@ +export { EffortSliderCard } from "./EffortSliderCard"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelFlyout/ModelFlyout.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelFlyout/ModelFlyout.tsx deleted file mode 100644 index fe0c559d55f..00000000000 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelFlyout/ModelFlyout.tsx +++ /dev/null @@ -1,262 +0,0 @@ -import { useLingui } from "@lingui/react/macro"; -import type { SessionConfigOption } from "@superset/chat/protocol"; -import { DropdownMenuItem } from "@superset/ui/dropdown-menu"; -import { cn } from "@superset/ui/utils"; -import { type RefObject, useEffect, useRef, useState } from "react"; -import { LuCheck, LuSearch, LuStar } from "react-icons/lu"; -import { - getPresetIcon, - useIsDarkTheme, -} from "renderer/assets/app-icons/preset-icons"; -import { MENU_LABEL_CLASS, MENU_ROW_CLASS } from "../../../../constants"; -import { useFavoriteModels } from "../../hooks/useFavoriteModels"; -import type { AgentSwitcher } from "../../types"; - -type Row = { - presetId: string; - id: string | null; - label: string; - description?: string; - agentLabel?: string; -}; - -const FAVORITES_TAB = "favorites"; - -export function ModelFlyout({ - agentSwitcher, - model, - onPick, - searchRef, -}: { - agentSwitcher?: AgentSwitcher; - model: SessionConfigOption | undefined; - onPick: (modelId: string) => void; - searchRef: RefObject; -}) { - const { t } = useLingui(); - const isDark = useIsDarkTheme(); - const currentPresetId = agentSwitcher?.currentPresetId ?? ""; - const [query, setQuery] = useState(""); - const [tab, setTab] = useState(currentPresetId); - const { favorites, toggle } = useFavoriteModels(); - const listRef = useRef(null); - - useEffect(() => { - const frame = requestAnimationFrame(() => searchRef.current?.focus()); - return () => cancelAnimationFrame(frame); - }, [searchRef]); - - const rowKey = (row: Row) => `${row.presetId}:${row.id ?? ""}`; - const isFavorite = (row: Row) => - favorites.some( - (favorite) => - favorite.presetId === row.presetId && favorite.id === row.id, - ); - const rowsFor = (presetId: string): Row[] => { - if (presetId === currentPresetId && model) { - return model.options.map((option) => ({ ...option, presetId })); - } - const models = - presetId === currentPresetId - ? [] - : (agentSwitcher?.agents.find((agent) => agent.presetId === presetId) - ?.models ?? []); - return models.length > 0 - ? models.map((entry) => ({ ...entry, presetId })) - : [{ presetId, id: null, label: t({ message: "Default" }) }]; - }; - const onFavoritesTab = tab === FAVORITES_TAB; - const rows: Row[] = onFavoritesTab - ? favorites.flatMap((favorite) => { - const agent = agentSwitcher?.agents.find( - (entry) => entry.presetId === favorite.presetId, - ); - return agent ? [{ ...favorite, agentLabel: agent.label }] : []; - }) - : rowsFor(tab); - const pickRow = (row: Row) => { - if (row.presetId === currentPresetId) { - if (row.id) onPick(row.id); - return; - } - agentSwitcher?.onSwitch( - row.presetId, - row.id ? { id: row.id, label: row.label } : null, - ); - }; - - const needle = query.trim().toLowerCase(); - const matches = rows.filter((row) => - `${row.label} ${row.agentLabel ?? ""}`.toLowerCase().includes(needle), - ); - const starred = onFavoritesTab ? [] : matches.filter(isFavorite); - const rest = onFavoritesTab - ? matches - : matches.filter((row) => !isFavorite(row)); - const groups = [ - { id: "favorites", label: t({ message: "Favorites" }), rows: starred }, - { - id: "all", - label: starred.length > 0 ? t({ message: "All models" }) : null, - rows: rest, - }, - ].filter((group) => group.rows.length > 0); - const selectTab = (next: string) => { - if (next === tab) return; - setTab(next); - setQuery(""); - }; - - const renderRow = (row: Row) => { - const favorited = isFavorite(row); - return ( - pickRow(row)} - title={row.description} - > - {row.label} - {row.agentLabel ? ( - - {row.agentLabel} - - ) : null} - - - {row.presetId === currentPresetId && - row.id === (model?.currentValue ?? null) ? ( - - ) : null} - - - ); - }; - - return ( - <> - {agentSwitcher ? ( -
- {[ - { presetId: FAVORITES_TAB, label: t({ message: "Favorites" }) }, - ...agentSwitcher.agents, - ].map((entry) => { - const selected = entry.presetId === tab; - const icon = - entry.presetId === FAVORITES_TAB - ? null - : getPresetIcon(entry.presetId, isDark); - return ( - - ); - })} -
- ) : null} -
- -
- {groups.length === 0 ? ( -
- {onFavoritesTab && !needle - ? t({ message: "No favorite models" }) - : t({ message: "No matching models" })} -
- ) : ( - groups.map((group) => ( -
- {group.label ? ( -
{group.label}
- ) : null} - {group.rows.map(renderRow)} -
- )) - )} -
-
- - ); -} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelFlyout/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelFlyout/index.ts deleted file mode 100644 index 1c2e70fdb3c..00000000000 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelFlyout/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { ModelFlyout } from "./ModelFlyout"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelPanel/ModelPanel.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelPanel/ModelPanel.tsx new file mode 100644 index 00000000000..e7d690a2063 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelPanel/ModelPanel.tsx @@ -0,0 +1,327 @@ +import { useLingui } from "@lingui/react/macro"; +import type { SessionConfigOption } from "@superset/chat/protocol"; +import { DropdownMenuItem } from "@superset/ui/dropdown-menu"; +import { cn } from "@superset/ui/utils"; +import { + type KeyboardEvent, + type RefObject, + useEffect, + useRef, + useState, +} from "react"; +import { LuCheck, LuSearch, LuStar } from "react-icons/lu"; +import { + getPresetIcon, + useIsDarkTheme, +} from "renderer/assets/app-icons/preset-icons"; +import { MENU_ROW_CLASS } from "../../../../constants"; +import { useFavoriteModels } from "../../hooks/useFavoriteModels"; +import type { AgentSwitcher } from "../../types"; + +type Row = { + presetId: string; + id: string | null; + label: string; + description?: string; + agentLabel?: string; +}; + +const FAVORITES_TAB = "favorites"; +const SHORTCUT_ROW_LIMIT = 9; +const SHORTCUT_MODIFIER = + typeof navigator !== "undefined" && + navigator.platform.toLowerCase().includes("mac") + ? "⌘" + : "Ctrl "; + +export function ModelPanel({ + agentSwitcher, + model, + onLeave, + onPick, + searchRef, +}: { + agentSwitcher?: AgentSwitcher; + model: SessionConfigOption | undefined; + /** Tab past the last agent tab (or Shift+Tab before the first) leaves the panel. */ + onLeave?: (direction: 1 | -1) => void; + onPick: (modelId: string) => void; + searchRef: RefObject; +}) { + const { t } = useLingui(); + const isDark = useIsDarkTheme(); + const currentPresetId = agentSwitcher?.currentPresetId ?? ""; + const [query, setQuery] = useState(""); + const [tab, setTab] = useState(currentPresetId); + const { favorites, toggle } = useFavoriteModels(); + const listRef = useRef(null); + + useEffect(() => { + const frame = requestAnimationFrame(() => searchRef.current?.focus()); + return () => cancelAnimationFrame(frame); + }, [searchRef]); + + const rowKey = (row: Row) => `${row.presetId}:${row.id ?? ""}`; + const isFavorite = (row: Row) => + favorites.some( + (favorite) => + favorite.presetId === row.presetId && favorite.id === row.id, + ); + const rowsFor = (presetId: string): Row[] => { + if (presetId === currentPresetId && model) { + return model.options.map((option) => ({ ...option, presetId })); + } + const models = + presetId === currentPresetId + ? [] + : (agentSwitcher?.agents.find((agent) => agent.presetId === presetId) + ?.models ?? []); + return models.length > 0 + ? models.map((entry) => ({ ...entry, presetId })) + : [{ presetId, id: null, label: t({ message: "Default" }) }]; + }; + const onFavoritesTab = tab === FAVORITES_TAB; + const rows: Row[] = onFavoritesTab + ? favorites.flatMap((favorite) => { + const agent = agentSwitcher?.agents.find( + (entry) => entry.presetId === favorite.presetId, + ); + return agent ? [{ ...favorite, agentLabel: agent.label }] : []; + }) + : rowsFor(tab); + const pickRow = (row: Row) => { + if (row.presetId === currentPresetId) { + if (row.id) onPick(row.id); + return; + } + agentSwitcher?.onSwitch( + row.presetId, + row.id ? { id: row.id, label: row.label } : null, + ); + }; + + const needle = query.trim().toLowerCase(); + const matches = rows.filter((row) => + `${row.label} ${row.agentLabel ?? ""}`.toLowerCase().includes(needle), + ); + const starred = onFavoritesTab ? [] : matches.filter(isFavorite); + const rest = onFavoritesTab + ? matches + : matches.filter((row) => !isFavorite(row)); + const groups = [ + { id: "favorites", label: t({ message: "Favorites" }), rows: starred }, + { + id: "all", + label: starred.length > 0 ? t({ message: "All models" }) : null, + rows: rest, + }, + ].filter((group) => group.rows.length > 0); + const orderedRows = groups.flatMap((group) => group.rows); + const tabs = agentSwitcher + ? [ + { presetId: FAVORITES_TAB, label: t({ message: "Favorites" }) }, + ...agentSwitcher.agents, + ] + : []; + const selectTab = (next: string) => { + if (next === tab) return; + setTab(next); + setQuery(""); + }; + const cycleTab = (direction: 1 | -1) => { + const index = tabs.findIndex((entry) => entry.presetId === tab); + const target = index + direction; + if (onLeave && (target < 0 || target >= tabs.length)) { + onLeave(direction); + return; + } + const next = tabs[(target + tabs.length) % tabs.length]; + if (next) selectTab(next.presetId); + }; + // Tab walks the agent tabs instead of leaving the menu; mod+digit picks a row. + const onPanelKeyDown = (event: KeyboardEvent) => { + if (event.key === "Tab") { + if (tabs.length === 0 && !onLeave) return; + event.preventDefault(); + event.stopPropagation(); + cycleTab(event.shiftKey ? -1 : 1); + return; + } + if (!(event.metaKey || event.ctrlKey) || event.altKey) return; + const digit = Number(event.key); + if (!Number.isInteger(digit) || digit < 1 || digit > SHORTCUT_ROW_LIMIT) { + return; + } + const row = orderedRows[digit - 1]; + if (!row) return; + event.preventDefault(); + event.stopPropagation(); + pickRow(row); + }; + + const renderRow = (row: Row, index: number) => { + const favorited = isFavorite(row); + const selected = + row.presetId === currentPresetId && + row.id === (model?.currentValue ?? null); + return ( + pickRow(row)} + title={row.description} + > + {row.label} + {row.agentLabel ? ( + + {row.agentLabel} + + ) : null} + {index < SHORTCUT_ROW_LIMIT ? ( + + {SHORTCUT_MODIFIER} + {index + 1} + + ) : null} + + {selected ? : null} + + + + ); + }; + + let rowIndex = 0; + return ( +
+ {tabs.length > 0 ? ( +
+ {tabs.map((entry) => { + const selected = entry.presetId === tab; + const icon = + entry.presetId === FAVORITES_TAB + ? null + : getPresetIcon(entry.presetId, isDark); + return ( + + ); + })} +
+ ) : null} + +
+ {groups.length === 0 ? ( +
+ {onFavoritesTab && !needle + ? t({ message: "No favorite models" }) + : t({ message: "No matching models" })} +
+ ) : ( + groups.map((group) => ( +
+ {group.label ? ( +
+ {group.label} +
+ ) : null} + {group.rows.map((row) => renderRow(row, rowIndex++))} +
+ )) + )} +
+
+ ); +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelPanel/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelPanel/index.ts new file mode 100644 index 00000000000..6de55d62c96 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/components/ModelPicker/components/ModelPanel/index.ts @@ -0,0 +1 @@ +export { ModelPanel } from "./ModelPanel"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/constants.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/constants.ts index 7a2733586ae..4ace8b0c621 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/constants.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/constants.ts @@ -1,12 +1,13 @@ export const PILL_TRIGGER_CLASS = - "flex h-[26px] min-w-0 shrink cursor-pointer items-center gap-1 whitespace-nowrap rounded-md bg-foreground/[0.07] px-1.5 text-[11px] text-foreground outline-none transition-colors hover:bg-foreground/[0.11] focus-visible:ring-1 focus-visible:ring-ring data-[state=open]:bg-foreground/[0.11]"; + "flex h-7 min-w-0 shrink cursor-pointer items-center gap-1.5 whitespace-nowrap rounded-lg px-2 text-[12px] text-foreground/80 outline-none transition-colors hover:bg-foreground/[0.07] hover:text-foreground focus-visible:ring-1 focus-visible:ring-ring data-[state=open]:bg-foreground/[0.07] data-[state=open]:text-foreground"; export const PILL_CHEVRON_CLASS = - "size-3 shrink-0 text-muted-foreground transition-transform group-data-[state=open]:rotate-180"; + "size-3 shrink-0 opacity-60 transition-transform group-data-[state=open]:rotate-180"; -export const MENU_LABEL_CLASS = - "px-2 pt-2 pb-1 text-[10px] font-medium uppercase tracking-wide text-muted-foreground/70"; +/** Frosted picker panel shared by the composer's model and mode menus. */ +export const MENU_PANEL_CLASS = + "rounded-[0.875rem] border-border/80 bg-popover/85 shadow-lg backdrop-blur-2xl backdrop-saturate-150"; -export const MENU_ROW_CLASS = "h-8 gap-2 rounded-md px-2 text-xs"; +export const MENU_ROW_CLASS = "h-7 gap-2 rounded-[0.625rem] px-2 text-[13px]"; -export const MENU_DESCRIPTION_CLASS = "text-[11px] leading-snug"; +export const MENU_DESCRIPTION_CLASS = "text-[12px] leading-snug"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/index.ts new file mode 100644 index 00000000000..00a96c10c9b --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/index.ts @@ -0,0 +1 @@ +export { useOptimisticSelections } from "./useOptimisticSelections"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/useOptimisticSelections.test.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/useOptimisticSelections.test.tsx new file mode 100644 index 00000000000..c50daf55c2b --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/useOptimisticSelections.test.tsx @@ -0,0 +1,54 @@ +import { afterEach, expect, test } from "bun:test"; + +const { act, cleanup, renderHook } = await import("@testing-library/react"); +const { useOptimisticSelections } = await import("./useOptimisticSelections"); + +afterEach(cleanup); + +const settle = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); + +test("a pick shows at once and commits", () => { + let committed = 0; + const hook = renderHook(() => useOptimisticSelections({ effort: "high" })); + + act(() => + hook.result.current.select("effort", "max", () => { + committed += 1; + }), + ); + + expect(hook.result.current.shown("effort")).toBe("max"); + expect(committed).toBe(1); +}); + +test("the settled value takes over once it moves off the picked-against one", () => { + const hook = renderHook(({ effort }) => useOptimisticSelections({ effort }), { + initialProps: { effort: "high" }, + }); + act(() => hook.result.current.select("effort", "max", () => {})); + + hook.rerender({ effort: "max" }); + expect(hook.result.current.shown("effort")).toBe("max"); + + hook.rerender({ effort: "low" }); + expect(hook.result.current.shown("effort")).toBe("low"); +}); + +test("a pick the agent never echoes reverts after the settle delay", async () => { + const hook = renderHook(() => useOptimisticSelections({ mode: "ask" }, 20)); + act(() => hook.result.current.select("mode", "auto", () => {})); + expect(hook.result.current.shown("mode")).toBe("auto"); + + await act(() => settle(40)); + + expect(hook.result.current.shown("mode")).toBe("ask"); +}); + +test("a newer pick on the same key replaces the older one", () => { + const hook = renderHook(() => useOptimisticSelections({ effort: "high" })); + act(() => hook.result.current.select("effort", "max", () => {})); + act(() => hook.result.current.select("effort", "low", () => {})); + + expect(hook.result.current.shown("effort")).toBe("low"); +}); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/useOptimisticSelections.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/useOptimisticSelections.ts new file mode 100644 index 00000000000..20e4780571f --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useOptimisticSelections/useOptimisticSelections.ts @@ -0,0 +1,70 @@ +import { useCallback, useEffect, useRef, useState } from "react"; + +type Pending = { value: string; base: string | undefined }; + +export type OptimisticSelections = { + /** The settled value, or the picked one while the agent has not echoed it. */ + shown: (key: string) => string | undefined; + /** Show `value` for `key` now; `commit` sends it to the agent. */ + select: (key: string, value: string, commit: () => void) => void; +}; + +/** + * Session config and mode changes round-trip through the agent before the + * snapshot reflects them, so a controlled picker would show the old value for + * a few hundred milliseconds. A pick is shown at once and dropped when the + * settled value moves off the one it was picked against — the echo arrived, + * or something else changed it — or after `settleMs` if nothing ever comes. + */ +export function useOptimisticSelections( + settled: Record, + settleMs = 2000, +): OptimisticSelections { + const [pending, setPending] = useState>( + () => new Map(), + ); + const timers = useRef(new Map>()); + + useEffect(() => { + const active = timers.current; + return () => { + for (const timer of active.values()) clearTimeout(timer); + }; + }, []); + + const forget = useCallback((key: string, entry: Pending) => { + timers.current.delete(key); + setPending((current) => { + if (current.get(key) !== entry) return current; + const next = new Map(current); + next.delete(key); + return next; + }); + }, []); + + const select = useCallback( + (key: string, value: string, commit: () => void) => { + const entry: Pending = { value, base: settled[key] }; + setPending((current) => new Map(current).set(key, entry)); + const existing = timers.current.get(key); + if (existing) clearTimeout(existing); + timers.current.set( + key, + setTimeout(() => forget(key, entry), settleMs), + ); + commit(); + }, + [forget, settleMs, settled], + ); + + const shown = useCallback( + (key: string) => { + const entry = pending.get(key); + if (entry && settled[key] === entry.base) return entry.value; + return settled[key]; + }, + [pending, settled], + ); + + return { shown, select }; +} diff --git a/packages/i18n/locales/cs/messages.po b/packages/i18n/locales/cs/messages.po index a375a3ce92f..f8f8fd74680 100644 --- a/packages/i18n/locales/cs/messages.po +++ b/packages/i18n/locales/cs/messages.po @@ -7442,6 +7442,9 @@ msgstr "Selhání" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Rychlý" + msgid "Fatal" msgstr "Fatální" diff --git a/packages/i18n/locales/de/messages.po b/packages/i18n/locales/de/messages.po index 8838e2dd20a..ca529cde7bd 100644 --- a/packages/i18n/locales/de/messages.po +++ b/packages/i18n/locales/de/messages.po @@ -7442,6 +7442,9 @@ msgstr "Fehlgeschlagen" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Schnell" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/en/messages.po b/packages/i18n/locales/en/messages.po index 3dc5710acb9..1689a3613e9 100644 --- a/packages/i18n/locales/en/messages.po +++ b/packages/i18n/locales/en/messages.po @@ -7442,6 +7442,9 @@ msgstr "Failure" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Fast" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/es/messages.po b/packages/i18n/locales/es/messages.po index e93b14f116c..de480b5a633 100644 --- a/packages/i18n/locales/es/messages.po +++ b/packages/i18n/locales/es/messages.po @@ -7442,6 +7442,9 @@ msgstr "Fallo" msgid "FAQ" msgstr "Preguntas frecuentes" +msgid "Fast" +msgstr "Rápido" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/fr/messages.po b/packages/i18n/locales/fr/messages.po index 88f4bb86de4..40361bb9034 100644 --- a/packages/i18n/locales/fr/messages.po +++ b/packages/i18n/locales/fr/messages.po @@ -7442,6 +7442,9 @@ msgstr "Échec" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Rapide" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/id/messages.po b/packages/i18n/locales/id/messages.po index 02f2fc0bd7d..1cc016a58d7 100644 --- a/packages/i18n/locales/id/messages.po +++ b/packages/i18n/locales/id/messages.po @@ -7442,6 +7442,9 @@ msgstr "Gagal" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Cepat" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/it/messages.po b/packages/i18n/locales/it/messages.po index e0e8b38f1df..6cf9be91683 100644 --- a/packages/i18n/locales/it/messages.po +++ b/packages/i18n/locales/it/messages.po @@ -7442,6 +7442,9 @@ msgstr "Fallito" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Veloce" + msgid "Fatal" msgstr "Fatale" diff --git a/packages/i18n/locales/ja/messages.po b/packages/i18n/locales/ja/messages.po index 514dd035df5..01d8f68d754 100644 --- a/packages/i18n/locales/ja/messages.po +++ b/packages/i18n/locales/ja/messages.po @@ -7442,6 +7442,9 @@ msgstr "失敗" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "高速" + msgid "Fatal" msgstr "致命的" diff --git a/packages/i18n/locales/ko/messages.po b/packages/i18n/locales/ko/messages.po index 1fffdc69377..4c68a99cb69 100644 --- a/packages/i18n/locales/ko/messages.po +++ b/packages/i18n/locales/ko/messages.po @@ -7442,6 +7442,9 @@ msgstr "실패" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "빠름" + msgid "Fatal" msgstr "치명적" diff --git a/packages/i18n/locales/nl/messages.po b/packages/i18n/locales/nl/messages.po index 9340d6401b8..78c0fed27a3 100644 --- a/packages/i18n/locales/nl/messages.po +++ b/packages/i18n/locales/nl/messages.po @@ -7442,6 +7442,9 @@ msgstr "Mislukt" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Snel" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/pl/messages.po b/packages/i18n/locales/pl/messages.po index d50f1a59e07..dcf0e91eb24 100644 --- a/packages/i18n/locales/pl/messages.po +++ b/packages/i18n/locales/pl/messages.po @@ -7442,6 +7442,9 @@ msgstr "Niepowodzenie" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Szybki" + msgid "Fatal" msgstr "Krytyczny" diff --git a/packages/i18n/locales/pt-BR/messages.po b/packages/i18n/locales/pt-BR/messages.po index c7c60540dad..4ffa20f6841 100644 --- a/packages/i18n/locales/pt-BR/messages.po +++ b/packages/i18n/locales/pt-BR/messages.po @@ -7442,6 +7442,9 @@ msgstr "Falha" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Rápido" + msgid "Fatal" msgstr "Fatal" diff --git a/packages/i18n/locales/ru/messages.po b/packages/i18n/locales/ru/messages.po index 6b3a9cfd9e7..a97079c6d24 100644 --- a/packages/i18n/locales/ru/messages.po +++ b/packages/i18n/locales/ru/messages.po @@ -7442,6 +7442,9 @@ msgstr "Ошибка" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Быстрый" + msgid "Fatal" msgstr "Критическая" diff --git a/packages/i18n/locales/tr/messages.po b/packages/i18n/locales/tr/messages.po index c29502a2eb5..1bf3c40cb04 100644 --- a/packages/i18n/locales/tr/messages.po +++ b/packages/i18n/locales/tr/messages.po @@ -7442,6 +7442,9 @@ msgstr "Başarısız" msgid "FAQ" msgstr "SSS" +msgid "Fast" +msgstr "Hızlı" + msgid "Fatal" msgstr "Kritik" diff --git a/packages/i18n/locales/vi/messages.po b/packages/i18n/locales/vi/messages.po index d00d020ab7a..7e15bfb4fba 100644 --- a/packages/i18n/locales/vi/messages.po +++ b/packages/i18n/locales/vi/messages.po @@ -7442,6 +7442,9 @@ msgstr "Thất bại" msgid "FAQ" msgstr "FAQ" +msgid "Fast" +msgstr "Nhanh" + msgid "Fatal" msgstr "Nghiêm trọng" diff --git a/packages/i18n/locales/zh-CN/messages.po b/packages/i18n/locales/zh-CN/messages.po index b0baf94079c..8b48ce5073d 100644 --- a/packages/i18n/locales/zh-CN/messages.po +++ b/packages/i18n/locales/zh-CN/messages.po @@ -7442,6 +7442,9 @@ msgstr "失败" msgid "FAQ" msgstr "常见问题" +msgid "Fast" +msgstr "快速" + msgid "Fatal" msgstr "致命" diff --git a/packages/i18n/locales/zh-TW/messages.po b/packages/i18n/locales/zh-TW/messages.po index ff190c0b99e..535e8145f2d 100644 --- a/packages/i18n/locales/zh-TW/messages.po +++ b/packages/i18n/locales/zh-TW/messages.po @@ -7442,6 +7442,9 @@ msgstr "失敗" msgid "FAQ" msgstr "常見問題" +msgid "Fast" +msgstr "快速" + msgid "Fatal" msgstr "致命" diff --git a/packages/ui/src/components/ui/slider.tsx b/packages/ui/src/components/ui/slider.tsx index 669baf208eb..8f975424cba 100644 --- a/packages/ui/src/components/ui/slider.tsx +++ b/packages/ui/src/components/ui/slider.tsx @@ -7,12 +7,22 @@ import { cn } from "../../lib/utils"; function Slider({ className, + trackClassName, + rangeClassName, + thumbClassName, + children, defaultValue, value, min = 0, max = 100, ...props -}: React.ComponentProps) { +}: React.ComponentProps & { + trackClassName?: string; + rangeClassName?: string; + thumbClassName?: string; + /** Rendered inside the track, after the range (e.g. step marks). */ + children?: React.ReactNode; +}) { const _values = React.useMemo( () => Array.isArray(value) @@ -40,21 +50,27 @@ function Slider({ data-slot="slider-track" className={cn( "bg-muted relative grow overflow-hidden rounded-full data-[orientation=horizontal]:h-1.5 data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-1.5", + trackClassName, )} > + {children} {Array.from({ length: _values.length }, (_, index) => ( ))}