Session config surface replaces the row controls

This commit is contained in:
Chris Scott committed 2026-11-26 23:48:09 -05:00
1 parent 750faf1361
commit 13eca93b92
16 files changed
+1035 -1102

No files matched your search

@@ -0,0 +1,481 @@
import { Fragment, memo, useState, type ReactNode } from 'react'
import { Box, Cpu, Paperclip, Shield, ShieldCheck, SlidersHorizontal, Target } from 'lucide-react'
import type { AgentInfo } from '@opencode-manager/shared/opencode'
import { useAgents, useSession } from '@/hooks/useOpenCode'
import { useContextUsage } from '@/hooks/useContextUsage'
import { useVariants } from '@/hooks/useVariants'
import type { ModelSelectionSession } from '@/hooks/useModelSelection'
import { useMobile } from '@/hooks/useMobile'
import { usePermissionModeToggle } from '@/hooks/usePermissionModeToggle'
import { useSandboxToggle } from '@/hooks/useSandboxToggle'
import { isLocalOpenCodeServer, useOpenCodeServerId } from '@/contexts/opencode-server-context'
import { getPrimaryAgents } from '@/lib/primaryAgents'
import { getAgentStyleVars } from '@/lib/agent-colors'
import { shouldShowSandboxResources, formatSandboxResources } from '@/lib/sandboxResources'
import {
CONTEXT_USAGE_TEXT_CLASS,
contextUsageAccessibleLabel,
contextUsageTone,
formatContextUsage,
} from '@/lib/contextUsage'
import { cn } from '@/lib/utils'
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { BottomSheet, BottomSheetContent, BottomSheetHeader } from '@/components/ui/bottom-sheet'
import { getIconToggleButtonClassName } from '@/components/ui/icon-toggle-button'
import { ContextUsageMeter } from '@/components/session/ContextUsageMeter'
import { ModelQuickSelect } from '@/components/model/ModelQuickSelect'
import { SessionSandboxResourcesDialog } from './SessionSandboxResourcesDialog'
import { ToolMenuItemContent } from './ToolMenuItemContent'
interface SessionConfigMenuProps {
sessionID: string
directory?: string
currentAgent: string
isBashMode: boolean
onAgentChange: (agentId: string) => void
modelName: string
modelSelectionSession?: ModelSelectionSession
modelPickerOpen: boolean
onModelPickerOpenChange: (open: boolean) => void
isCompacting: boolean
onCompact: () => void
goalArmed: boolean
goalDisabled: boolean
goalLabel: string
onToggleGoal: () => void
onAttachFile: () => void
}
interface SessionConfigEntryBase {
key: string
title: string
detail?: string
disabled?: boolean
icon?: ReactNode
className?: string
}
type SessionConfigEntry =
| (SessionConfigEntryBase & { kind: 'radio'; checked: boolean; onSelect: () => void })
| (SessionConfigEntryBase & { kind: 'checkbox'; checked: boolean; onSelect: () => void })
| (SessionConfigEntryBase & { kind: 'action'; onSelect: () => void })
interface SessionConfigSection {
key: string
label: string
entries: SessionConfigEntry[]
}
type SelectableAgent = Pick<AgentInfo, 'id' | 'name' | 'color' | 'description'>
const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1).toLowerCase()
const findAgent = (agents: SelectableAgent[], agentId: string): SelectableAgent | undefined =>
agents.find((agent) => agent.id.toLowerCase() === agentId.toLowerCase())
const EMPTY_ICON = <span aria-hidden="true" className="block h-4 w-4" />
const TRIGGER_CLASS =
'relative px-2.5 py-0.5 md:px-3 min-h-11 md:min-h-9 min-w-0 rounded-lg text-xs md:text-sm font-medium border bg-muted border-border text-muted-foreground hover:bg-muted-foreground/10 hover:border-foreground/30 transition-colors cursor-pointer flex-1 md:flex-initial md:w-auto md:max-w-[260px] dark:border-border/30 flex flex-col items-start justify-center overflow-hidden'
export const SessionConfigMenu = memo(function SessionConfigMenu({
sessionID,
directory,
currentAgent,
isBashMode,
onAgentChange,
modelName,
modelSelectionSession,
modelPickerOpen,
onModelPickerOpenChange,
isCompacting,
onCompact,
goalArmed,
goalDisabled,
goalLabel,
onToggleGoal,
onAttachFile,
}: SessionConfigMenuProps) {
const isMobile = useMobile()
const { data: agents = [] } = useAgents(directory)
const { hasVariants, availableVariants, currentVariant, setVariant, clearVariant } = useVariants(
directory,
modelSelectionSession,
)
const { data: session } = useSession(sessionID, directory)
const contextUsage = useContextUsage(sessionID, directory, session?.revert?.messageID)
const isLocalServer = isLocalOpenCodeServer(useOpenCodeServerId())
const permission = usePermissionModeToggle(sessionID, directory ?? '', { enabled: directory !== undefined })
const sandbox = useSandboxToggle(sessionID, { enabled: isLocalServer })
const [open, setOpen] = useState(false)
const [resourcesOpen, setResourcesOpen] = useState(false)
const currentAgentInfo = findAgent(agents, currentAgent)
const agentLabel = isBashMode ? 'Bash' : capitalize(currentAgentInfo?.name ?? currentAgent)
const agentStyleVars = getAgentStyleVars(
isBashMode ? 'plan' : currentAgent,
isBashMode ? undefined : currentAgentInfo?.color,
)
const usageLabel = contextUsage ? formatContextUsage(contextUsage) : null
const usageTone = CONTEXT_USAGE_TEXT_CLASS[contextUsageTone(contextUsage?.usagePercentage ?? null)]
const showVariant = hasVariants && Boolean(currentVariant)
const isAnySessionToggleActive = permission.isAuto || sandbox.isSandboxed || goalArmed
const configName = `Session config: ${[
agentLabel,
modelName || 'Select model',
showVariant ? `${currentVariant} variant` : null,
contextUsage ? contextUsageAccessibleLabel(contextUsage) : null,
].filter(Boolean).join(', ')}`
const sections: SessionConfigSection[] = [
{
key: 'agent',
label: 'Agent',
entries: getPrimaryAgents(agents).map((agent) => ({
kind: 'radio',
key: `agent:${agent.id}`,
title: capitalize(agent.name),
detail: agent.description || undefined,
checked: agent.id.toLowerCase() === currentAgent.toLowerCase(),
onSelect: () => onAgentChange(agent.id),
})),
},
{
key: 'model',
label: 'Model',
entries: [
{
kind: 'action',
key: 'model',
title: modelName || 'Select model',
detail: 'Change model',
onSelect: () => {
setOpen(false)
setTimeout(() => onModelPickerOpenChange(true), 0)
},
},
],
},
]
if (hasVariants) {
sections.push({
key: 'variant',
label: 'Variant',
entries: [
{
kind: 'radio',
key: 'variant:default',
title: 'Default',
checked: currentVariant === undefined,
onSelect: clearVariant,
},
...availableVariants.map((variant) => ({
kind: 'radio' as const,
key: `variant:${variant}`,
title: variant,
className: 'capitalize',
checked: currentVariant === variant,
onSelect: () => setVariant(variant),
})),
],
})
}
sections.push({
key: 'context',
label: 'Context',
entries: [
{
kind: 'action',
key: 'context',
title: 'Compact session',
detail: contextUsage ? contextUsageAccessibleLabel(contextUsage) : undefined,
disabled: isCompacting,
onSelect: () => {
onCompact()
setOpen(false)
},
},
],
})
if (directory !== undefined) {
const sessionEntries: SessionConfigEntry[] = [
{
kind: 'checkbox',
key: 'permission',
title: 'Accept all permissions',
detail: permission.disabled ? permission.label : undefined,
checked: permission.isAuto,
disabled: permission.disabled,
icon: permission.isAuto ? <ShieldCheck className="h-4 w-4" /> : <Shield className="h-4 w-4" />,
onSelect: permission.toggle,
},
]
if (isLocalServer && sandbox.available) {
sessionEntries.push({
kind: 'checkbox',
key: 'sandbox',
title: 'Sandbox commands',
detail: sandbox.disabled ? sandbox.label : undefined,
checked: sandbox.isSandboxed,
disabled: sandbox.disabled,
icon: <Box className="h-4 w-4" />,
onSelect: sandbox.toggle,
})
if (shouldShowSandboxResources(sandbox)) {
sessionEntries.push({
kind: 'action',
key: 'sandbox-resources',
title: 'Sandbox resources…',
detail: `${formatSandboxResources(sandbox.effectiveResources!)}${sandbox.resources ? ' · custom' : ''}`,
icon: <Cpu className="h-4 w-4" />,
onSelect: () => {
setOpen(false)
setTimeout(() => setResourcesOpen(true), 0)
},
})
}
}
sessionEntries.push({
kind: 'checkbox',
key: 'goal',
title: 'Goal mode',
detail: goalDisabled ? goalLabel : undefined,
checked: goalArmed,
disabled: goalDisabled,
icon: <Target className="h-4 w-4" />,
onSelect: onToggleGoal,
})
sections.push({ key: 'session', label: 'Session', entries: sessionEntries })
}
sections.push({
key: 'attach',
label: '',
entries: [
{
kind: 'action',
key: 'attach',
title: 'Attach image or PDF',
icon: <Paperclip className="h-4 w-4" />,
onSelect: () => {
onAttachFile()
setOpen(false)
},
},
],
})
const renderDesktopEntry = (entry: SessionConfigEntry) => {
const fallbackClass = entry.kind === 'action' ? 'pl-8' : undefined
if (entry.kind === 'radio') {
return (
<DropdownMenuRadioItem
key={entry.key}
value={entry.key}
disabled={entry.disabled}
className={cn(fallbackClass, entry.className)}
onSelect={(event) => event.preventDefault()}
>
<ToolMenuItemContent icon={entry.icon ?? EMPTY_ICON} title={entry.title} detail={entry.detail} />
</DropdownMenuRadioItem>
)
}
if (entry.kind === 'checkbox') {
return (
<DropdownMenuCheckboxItem
key={entry.key}
checked={entry.checked}
disabled={entry.disabled}
className={cn(fallbackClass, entry.className)}
onSelect={(event) => event.preventDefault()}
onCheckedChange={() => entry.onSelect()}
>
<ToolMenuItemContent icon={entry.icon ?? EMPTY_ICON} title={entry.title} detail={entry.detail} />
</DropdownMenuCheckboxItem>
)
}
return (
<DropdownMenuItem
key={entry.key}
disabled={entry.disabled}
className={cn(fallbackClass, entry.className)}
onSelect={() => entry.onSelect()}
>
<ToolMenuItemContent icon={entry.icon ?? EMPTY_ICON} title={entry.title} detail={entry.detail} />
</DropdownMenuItem>
)
}
const renderDesktopSection = (section: SessionConfigSection) => {
const checkedKey = section.entries.find((entry) => entry.kind === 'radio' && entry.checked)?.key
const hasRadio = section.entries.some((entry) => entry.kind === 'radio')
if (hasRadio) {
return (
<DropdownMenuRadioGroup
value={checkedKey}
onValueChange={(value) => {
section.entries.find((entry) => entry.kind === 'radio' && entry.key === value)?.onSelect()
}}
>
{section.entries.map(renderDesktopEntry)}
</DropdownMenuRadioGroup>
)
}
return section.entries.map(renderDesktopEntry)
}
const meter = contextUsage
? <ContextUsageMeter percentage={contextUsage.usagePercentage} className="absolute inset-x-0 bottom-0" />
: null
const desktopTrigger = (
<button
type="button"
aria-label={configName}
title={configName}
data-active={isAnySessionToggleActive ? 'true' : undefined}
className={TRIGGER_CLASS}
>
<span className="flex w-full min-w-0 items-center gap-1 text-left">
<SlidersHorizontal className={cn('h-3.5 w-3.5 shrink-0', isAnySessionToggleActive && 'text-highlight')} />
<span className="truncate">
<span style={{ color: agentStyleVars['--agent-color'] }}>{agentLabel}</span>
<span> · {modelName || 'Select model'}</span>
</span>
</span>
{(showVariant || usageLabel) && (
<span className="flex w-full min-w-0 items-center gap-1 text-[10px] leading-tight">
{showVariant && <span className="truncate capitalize text-highlight">{currentVariant}</span>}
{showVariant && usageLabel && (
<span aria-hidden="true" className="text-muted-foreground">·</span>
)}
{usageLabel && (
<span data-testid="composer-config-context-usage" className={cn('shrink-0 font-medium', usageTone)}>
{usageLabel}
</span>
)}
</span>
)}
{meter}
</button>
)
return (
<>
{isMobile ? (
<>
<button
type="button"
aria-label={configName}
title={configName}
data-active={isAnySessionToggleActive ? 'true' : undefined}
onClick={() => setOpen(true)}
className={cn(
getIconToggleButtonClassName(isAnySessionToggleActive),
'relative flex items-center justify-center size-11 p-0 overflow-hidden',
)}
>
<SlidersHorizontal className="h-5 w-5" />
{meter}
</button>
<BottomSheet
isOpen={open}
onClose={() => setOpen(false)}
ariaLabel="Session config"
heightClass="h-auto max-h-[85dvh]"
>
<BottomSheetHeader title="Session config" />
<BottomSheetContent>
<div role="menu" aria-label="Session config">
{sections.map((section, index) => (
<div key={section.key} className={index > 0 ? 'mt-2 border-t border-border pt-2' : ''}>
{section.label ? (
<div role="presentation" className="px-2 py-1 text-sm font-semibold text-muted-foreground">
{section.label}
</div>
) : null}
{section.entries.map((entry) => (
<button
key={entry.key}
type="button"
role={entry.kind === 'radio' ? 'menuitemradio' : entry.kind === 'checkbox' ? 'menuitemcheckbox' : 'menuitem'}
aria-checked={entry.kind === 'action' ? undefined : entry.checked}
disabled={entry.disabled}
aria-disabled={entry.disabled ? 'true' : undefined}
onClick={entry.onSelect}
className={cn(
'flex w-full min-h-11 items-center rounded-lg px-2 py-2 text-left text-sm hover:bg-accent',
entry.className,
)}
>
<ToolMenuItemContent icon={entry.icon ?? EMPTY_ICON} title={entry.title} detail={entry.detail} />
</button>
))}
</div>
))}
</div>
</BottomSheetContent>
</BottomSheet>
</>
) : (
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger asChild>{desktopTrigger}</DropdownMenuTrigger>
<DropdownMenuContent
side="top"
align="start"
collisionPadding={8}
className="w-72 max-h-[70vh] overflow-y-auto"
>
{sections.map((section, index) => (
<Fragment key={section.key}>
{index > 0 ? <DropdownMenuSeparator /> : null}
{section.label ? <DropdownMenuLabel>{section.label}</DropdownMenuLabel> : null}
{renderDesktopSection(section)}
</Fragment>
))}
</DropdownMenuContent>
</DropdownMenu>
)}
<ModelQuickSelect
directory={directory}
open={modelPickerOpen}
onOpenChange={onModelPickerOpenChange}
session={modelSelectionSession}
contextUsage={contextUsage}
isCompacting={isCompacting}
onCompact={onCompact}
/>
{shouldShowSandboxResources(sandbox) ? (
<SessionSandboxResourcesDialog
sessionID={sessionID}
open={resourcesOpen}
onOpenChange={setResourcesOpen}
override={sandbox.resources}
effective={sandbox.effectiveResources!}
/>
) : null}
</>
)
})