mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(sandbox): add the session sandbox control to the desktop composer
The sandbox toggle and resources dialog were only reachable from the mobile composer menu. Add SessionSandboxMenu next to the permission toggle on desktop and share the sandbox menu items with the mobile menu.
This commit is contained in:
1 parent
b21fdf5b94
commit
95a9f6e1ed
11 files changed
+278
-45
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useState, type ReactNode } from 'react'
|
||||
import { Box, Cpu, Mic, Paperclip, Plus, Shield, ShieldCheck, Target } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { Mic, Paperclip, Plus, Shield, ShieldCheck, Target } from 'lucide-react'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuCheckboxItem,
|
||||
@@ -11,7 +11,9 @@ import {
|
||||
import { getIconToggleButtonClassName } from '@/components/ui/icon-toggle-button'
|
||||
import { usePermissionModeToggle } from '@/hooks/usePermissionModeToggle'
|
||||
import { useSandboxToggle } from '@/hooks/useSandboxToggle'
|
||||
import { formatSandboxResources } from '@/lib/sandboxResources'
|
||||
import { shouldShowSandboxResources } from '@/lib/sandboxResources'
|
||||
import { SandboxMenuItems } from './SandboxMenuItems'
|
||||
import { ToolMenuItemContent } from './ToolMenuItemContent'
|
||||
import { SessionSandboxResourcesDialog } from './SessionSandboxResourcesDialog'
|
||||
|
||||
interface ComposerToolsMenuProps {
|
||||
@@ -39,9 +41,6 @@ export function ComposerToolsMenu({
|
||||
const sandbox = useSandboxToggle(sessionID)
|
||||
const [resourcesOpen, setResourcesOpen] = useState(false)
|
||||
|
||||
const showSandboxResources =
|
||||
sandbox.available && sandbox.isSandboxed && sandbox.lockedReason !== 'child' && sandbox.effectiveResources !== undefined
|
||||
|
||||
return (
|
||||
<>
|
||||
<DropdownMenu>
|
||||
@@ -67,32 +66,7 @@ export function ComposerToolsMenu({
|
||||
detail={permission.disabled ? permission.label : undefined}
|
||||
/>
|
||||
</DropdownMenuCheckboxItem>
|
||||
{sandbox.available ? (
|
||||
<DropdownMenuCheckboxItem
|
||||
checked={sandbox.isSandboxed}
|
||||
disabled={sandbox.disabled}
|
||||
onCheckedChange={sandbox.toggle}
|
||||
>
|
||||
<ToolMenuItemContent
|
||||
icon={<Box className="h-4 w-4" />}
|
||||
title="Sandbox commands"
|
||||
detail={sandbox.disabled ? sandbox.label : undefined}
|
||||
/>
|
||||
</DropdownMenuCheckboxItem>
|
||||
) : null}
|
||||
{showSandboxResources ? (
|
||||
<DropdownMenuItem
|
||||
className="pl-8"
|
||||
aria-haspopup="dialog"
|
||||
onSelect={() => setTimeout(() => setResourcesOpen(true), 0)}
|
||||
>
|
||||
<ToolMenuItemContent
|
||||
icon={<Cpu className="h-4 w-4" />}
|
||||
title="Sandbox resources…"
|
||||
detail={`${formatSandboxResources(sandbox.effectiveResources!)}${sandbox.resources ? ' · custom' : ''}`}
|
||||
/>
|
||||
</DropdownMenuItem>
|
||||
) : null}
|
||||
<SandboxMenuItems sandbox={sandbox} onOpenResources={() => setResourcesOpen(true)} />
|
||||
<DropdownMenuCheckboxItem checked={goalArmed} disabled={goalDisabled} onCheckedChange={onToggleGoal}>
|
||||
<ToolMenuItemContent
|
||||
icon={<Target className="h-4 w-4" />}
|
||||
@@ -111,7 +85,7 @@ export function ComposerToolsMenu({
|
||||
) : null}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
{showSandboxResources ? (
|
||||
{shouldShowSandboxResources(sandbox) ? (
|
||||
<SessionSandboxResourcesDialog
|
||||
sessionID={sessionID}
|
||||
open={resourcesOpen}
|
||||
@@ -123,15 +97,3 @@ export function ComposerToolsMenu({
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function ToolMenuItemContent({ icon, title, detail }: { icon: ReactNode; title: string; detail?: string }) {
|
||||
return (
|
||||
<span className="flex items-start gap-2">
|
||||
<span className="mt-0.5 shrink-0">{icon}</span>
|
||||
<span className="flex min-w-0 flex-col">
|
||||
<span>{title}</span>
|
||||
{detail ? <span className="text-xs text-muted-foreground">{detail}</span> : null}
|
||||
</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -111,6 +111,10 @@ vi.mock('./ComposerToolsMenu', () => ({
|
||||
ComposerToolsMenu: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/SessionSandboxMenu', () => ({
|
||||
SessionSandboxMenu: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('./PromptSuggestions', () => ({
|
||||
PromptSuggestions: (props: { isOpen: boolean; selectedIndex: number }) => {
|
||||
if (props.isOpen) {
|
||||
|
||||
@@ -118,6 +118,10 @@ vi.mock('./ComposerToolsMenu', () => ({
|
||||
),
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/SessionSandboxMenu', () => ({
|
||||
SessionSandboxMenu: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('@/components/ui/session-status-indicator', () => ({
|
||||
SessionStatusIndicator: () => <div>SessionStatus</div>,
|
||||
}))
|
||||
|
||||
@@ -107,6 +107,10 @@ vi.mock('./ComposerToolsMenu', () => ({
|
||||
ComposerToolsMenu: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/SessionSandboxMenu', () => ({
|
||||
SessionSandboxMenu: () => null,
|
||||
}))
|
||||
|
||||
const createTestQueryClient = () => new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
|
||||
@@ -108,6 +108,10 @@ vi.mock('./ComposerToolsMenu', () => ({
|
||||
ComposerToolsMenu: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('@/components/session/SessionSandboxMenu', () => ({
|
||||
SessionSandboxMenu: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSessionGoals', () => ({
|
||||
useSessionGoal: () => ({ data: undefined }),
|
||||
useStartSessionGoal: () => ({ mutateAsync: vi.fn(), isPending: false }),
|
||||
|
||||
@@ -29,6 +29,7 @@ import { ModelQuickSelect } from '@/components/model/ModelQuickSelect'
|
||||
import { AgentQuickSelect } from '@/components/agent/AgentQuickSelect'
|
||||
import { VoiceStatusOverlay, type VoiceStatusOverlayState } from './VoiceStatusOverlay'
|
||||
import { PermissionModeToggle } from '@/components/session/PermissionModeToggle'
|
||||
import { SessionSandboxMenu } from '@/components/session/SessionSandboxMenu'
|
||||
import { ComposerToolsMenu } from './ComposerToolsMenu'
|
||||
import { useSessionGoal, useStartSessionGoal } from '@/hooks/useSessionGoals'
|
||||
import { useSessionPermissionMode } from '@/hooks/useSessionPermissionMode'
|
||||
@@ -1579,6 +1580,7 @@ return (
|
||||
{directory && !isMobile && (
|
||||
<>
|
||||
<PermissionModeToggle sessionID={sessionID} directory={directory} />
|
||||
<SessionSandboxMenu sessionID={sessionID} />
|
||||
<IconToggleButton
|
||||
active={isGoalArmed}
|
||||
label={goalButtonLabel}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Box, Cpu } from 'lucide-react'
|
||||
import { DropdownMenuCheckboxItem, DropdownMenuItem } from '@/components/ui/dropdown-menu'
|
||||
import { formatSandboxResources, shouldShowSandboxResources, type SandboxToggleState } from '@/lib/sandboxResources'
|
||||
import { ToolMenuItemContent } from './ToolMenuItemContent'
|
||||
|
||||
interface SandboxMenuItemsProps {
|
||||
sandbox: SandboxToggleState
|
||||
onOpenResources: () => void
|
||||
}
|
||||
|
||||
export function SandboxMenuItems({ sandbox, onOpenResources }: SandboxMenuItemsProps) {
|
||||
return (
|
||||
<>
|
||||
{sandbox.available ? (
|
||||
<DropdownMenuCheckboxItem
|
||||
checked={sandbox.isSandboxed}
|
||||
disabled={sandbox.disabled}
|
||||
onCheckedChange={sandbox.toggle}
|
||||
>
|
||||
<ToolMenuItemContent
|
||||
icon={<Box className="h-4 w-4" />}
|
||||
title="Sandbox commands"
|
||||
detail={sandbox.disabled ? sandbox.label : undefined}
|
||||
/>
|
||||
</DropdownMenuCheckboxItem>
|
||||
) : null}
|
||||
{shouldShowSandboxResources(sandbox) ? (
|
||||
<DropdownMenuItem className="pl-8" aria-haspopup="dialog" onSelect={() => setTimeout(onOpenResources, 0)}>
|
||||
<ToolMenuItemContent
|
||||
icon={<Cpu className="h-4 w-4" />}
|
||||
title="Sandbox resources…"
|
||||
detail={`${formatSandboxResources(sandbox.effectiveResources!)}${sandbox.resources ? ' · custom' : ''}`}
|
||||
/>
|
||||
</DropdownMenuItem>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
interface ToolMenuItemContentProps {
|
||||
icon: ReactNode
|
||||
title: string
|
||||
detail?: string
|
||||
}
|
||||
|
||||
export function ToolMenuItemContent({ icon, title, detail }: ToolMenuItemContentProps) {
|
||||
return (
|
||||
<span className="flex items-start gap-2">
|
||||
<span className="mt-0.5 shrink-0">{icon}</span>
|
||||
<span className="flex min-w-0 flex-col">
|
||||
<span>{title}</span>
|
||||
{detail ? <span className="text-xs text-muted-foreground">{detail}</span> : null}
|
||||
</span>
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { SessionSandboxMenu } from './SessionSandboxMenu'
|
||||
import { useSessionSandbox, useSetSessionSandbox } from '@/hooks/useSessionSandbox'
|
||||
import type { SessionSandboxState } from '@opencode-manager/shared/schemas'
|
||||
|
||||
vi.mock('@/hooks/useSessionSandbox')
|
||||
|
||||
const baseSandboxState: SessionSandboxState = {
|
||||
sessionId: 'ses_1',
|
||||
rootSessionId: 'ses_1',
|
||||
sandboxed: false,
|
||||
source: 'default',
|
||||
resources: null,
|
||||
effectiveResources: { cpus: 2, memoryMib: 4096 },
|
||||
lockedReason: null,
|
||||
available: true,
|
||||
}
|
||||
|
||||
function mockSandbox(
|
||||
state: Partial<SessionSandboxState> = {},
|
||||
mutateAsync = vi.fn().mockResolvedValue(undefined),
|
||||
) {
|
||||
const mutate = vi.fn()
|
||||
vi.mocked(useSessionSandbox).mockReturnValue({
|
||||
data: { ...baseSandboxState, ...state },
|
||||
isError: false,
|
||||
} as ReturnType<typeof useSessionSandbox>)
|
||||
vi.mocked(useSetSessionSandbox).mockReturnValue({
|
||||
mutate,
|
||||
mutateAsync,
|
||||
isPending: false,
|
||||
} as unknown as ReturnType<typeof useSetSessionSandbox>)
|
||||
return { mutate, mutateAsync }
|
||||
}
|
||||
|
||||
function renderMenu() {
|
||||
render(<SessionSandboxMenu sessionID="ses_1" />)
|
||||
}
|
||||
|
||||
describe('SessionSandboxMenu', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockSandbox()
|
||||
})
|
||||
|
||||
it('renders nothing when sandboxing is unavailable', () => {
|
||||
mockSandbox({ available: false })
|
||||
renderMenu()
|
||||
|
||||
expect(screen.queryByRole('button', { name: /Sandbox:/ })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('reflects the off state in the trigger label', () => {
|
||||
mockSandbox({ sandboxed: false })
|
||||
renderMenu()
|
||||
|
||||
const trigger = screen.getByRole('button', { name: 'Sandbox: off' })
|
||||
expect(trigger).not.toHaveClass('bg-highlight')
|
||||
})
|
||||
|
||||
it('reflects the on state in the trigger label', () => {
|
||||
mockSandbox({ sandboxed: true })
|
||||
renderMenu()
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Sandbox: on' })).toHaveClass('bg-highlight')
|
||||
})
|
||||
|
||||
it('enables sandboxing through the checkbox item', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { mutate } = mockSandbox({ sandboxed: false })
|
||||
renderMenu()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Sandbox: off' }))
|
||||
const item = await screen.findByRole('menuitemcheckbox', { name: /Sandbox commands/ })
|
||||
expect(item).toHaveAttribute('aria-checked', 'false')
|
||||
await user.click(item)
|
||||
|
||||
expect(mutate).toHaveBeenCalledWith({ sandboxed: true })
|
||||
})
|
||||
|
||||
it('disables sandboxing through the checkbox item', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { mutate } = mockSandbox({ sandboxed: true })
|
||||
renderMenu()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Sandbox: on' }))
|
||||
const item = await screen.findByRole('menuitemcheckbox', { name: /Sandbox commands/ })
|
||||
expect(item).toHaveAttribute('aria-checked', 'true')
|
||||
await user.click(item)
|
||||
|
||||
expect(mutate).toHaveBeenCalledWith({ sandboxed: false })
|
||||
})
|
||||
|
||||
it('hides the resources item when the session is not sandboxed', async () => {
|
||||
const user = userEvent.setup()
|
||||
mockSandbox({ sandboxed: false })
|
||||
renderMenu()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Sandbox: off' }))
|
||||
await screen.findByRole('menuitemcheckbox', { name: /Sandbox commands/ })
|
||||
|
||||
expect(screen.queryByRole('menuitem', { name: /Sandbox resources/ })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('opens the resources dialog from the menu item', async () => {
|
||||
const user = userEvent.setup()
|
||||
mockSandbox({ sandboxed: true, effectiveResources: { cpus: 2, memoryMib: 4096 } })
|
||||
renderMenu()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Sandbox: on' }))
|
||||
const item = await screen.findByRole('menuitem', { name: /Sandbox resources/ })
|
||||
await user.click(item)
|
||||
|
||||
expect(await screen.findByRole('dialog')).toBeInTheDocument()
|
||||
expect(screen.getByLabelText('CPUs')).toHaveValue(2)
|
||||
})
|
||||
|
||||
it('disables the checkbox and explains inheritance for a child session', async () => {
|
||||
const user = userEvent.setup()
|
||||
mockSandbox({ sandboxed: true, lockedReason: 'child' })
|
||||
renderMenu()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Inherited from parent session/ }))
|
||||
const item = await screen.findByRole('menuitemcheckbox', { name: /Sandbox commands/ })
|
||||
|
||||
expect(item).toHaveAttribute('aria-disabled', 'true')
|
||||
expect(item).toHaveTextContent('Inherited from parent session')
|
||||
expect(screen.queryByRole('menuitem', { name: /Sandbox resources/ })).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,52 @@
|
||||
import { useState } from 'react'
|
||||
import { Box } from 'lucide-react'
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'
|
||||
import { getIconToggleButtonClassName } from '@/components/ui/icon-toggle-button'
|
||||
import { useSandboxToggle } from '@/hooks/useSandboxToggle'
|
||||
import { shouldShowSandboxResources } from '@/lib/sandboxResources'
|
||||
import { SandboxMenuItems } from '@/components/message/SandboxMenuItems'
|
||||
import { SessionSandboxResourcesDialog } from '@/components/message/SessionSandboxResourcesDialog'
|
||||
|
||||
interface SessionSandboxMenuProps {
|
||||
sessionID: string
|
||||
}
|
||||
|
||||
export function SessionSandboxMenu({ sessionID }: SessionSandboxMenuProps) {
|
||||
const sandbox = useSandboxToggle(sessionID)
|
||||
const [resourcesOpen, setResourcesOpen] = useState(false)
|
||||
|
||||
if (!sandbox.available) return null
|
||||
|
||||
const label = `Sandbox: ${sandbox.isSandboxed ? 'on' : 'off'}${
|
||||
sandbox.lockedReason === 'child' ? ` (${sandbox.label})` : ''
|
||||
}`
|
||||
|
||||
return (
|
||||
<>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
aria-label={label}
|
||||
title={label}
|
||||
className={getIconToggleButtonClassName(sandbox.isSandboxed)}
|
||||
>
|
||||
<Box className="w-5 h-5" />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent side="top" align="start" collisionPadding={8} className="w-64">
|
||||
<SandboxMenuItems sandbox={sandbox} onOpenResources={() => setResourcesOpen(true)} />
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
{shouldShowSandboxResources(sandbox) ? (
|
||||
<SessionSandboxResourcesDialog
|
||||
sessionID={sessionID}
|
||||
open={resourcesOpen}
|
||||
onOpenChange={setResourcesOpen}
|
||||
override={sandbox.resources}
|
||||
effective={sandbox.effectiveResources!}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -1,4 +1,16 @@
|
||||
import type { SessionSandboxResources } from '@opencode-manager/shared/schemas'
|
||||
import type { useSandboxToggle } from '@/hooks/useSandboxToggle'
|
||||
|
||||
export type SandboxToggleState = ReturnType<typeof useSandboxToggle>
|
||||
|
||||
export function shouldShowSandboxResources(sandbox: SandboxToggleState): boolean {
|
||||
return (
|
||||
sandbox.available &&
|
||||
sandbox.isSandboxed &&
|
||||
sandbox.lockedReason !== 'child' &&
|
||||
sandbox.effectiveResources !== undefined
|
||||
)
|
||||
}
|
||||
|
||||
export function formatSandboxMemory(memoryMib: number): string {
|
||||
const gib = memoryMib / 1024
|
||||
|
||||
Reference in new issue
Block a user