mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(frontend): consolidate session picker navigation and remote project loading
This commit is contained in:
1 parent
81fb2d4033
commit
52aeb7780f
42 files changed
+1486
-769
No files matched your search
@@ -0,0 +1,96 @@
|
||||
import { memo } from 'react'
|
||||
import { ModelQuickSelect } from '@/components/model/ModelQuickSelect'
|
||||
import { ContextUsageMeter } from '@/components/session/ContextUsageMeter'
|
||||
import { useSession } from '@/hooks/useOpenCode'
|
||||
import { useContextUsage } from '@/hooks/useContextUsage'
|
||||
import type { ModelSelectionSession } from '@/hooks/useModelSelection'
|
||||
import {
|
||||
CONTEXT_USAGE_TEXT_CLASS,
|
||||
contextUsageAccessibleLabel,
|
||||
contextUsageTone,
|
||||
formatContextUsage,
|
||||
} from '@/lib/contextUsage'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface ComposerModelPillProps {
|
||||
directory?: string
|
||||
sessionID: string
|
||||
modelName: string
|
||||
variant: string | null
|
||||
modelSelectionSession?: ModelSelectionSession
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
isCompacting: boolean
|
||||
onCompact: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Model selector trigger for the composer.
|
||||
*
|
||||
* Owns the context usage observer so transcript streaming flushes re-render only this pill instead of the
|
||||
* whole composer subtree.
|
||||
*/
|
||||
export const ComposerModelPill = memo(function ComposerModelPill({
|
||||
directory,
|
||||
sessionID,
|
||||
modelName,
|
||||
variant,
|
||||
modelSelectionSession,
|
||||
open,
|
||||
onOpenChange,
|
||||
isCompacting,
|
||||
onCompact,
|
||||
}: ComposerModelPillProps) {
|
||||
const { data: session } = useSession(sessionID, directory)
|
||||
const contextUsage = useContextUsage(sessionID, directory, session?.revert?.messageID)
|
||||
const pillUsage = contextUsage ? formatContextUsage(contextUsage) : null
|
||||
const pillUsageTone = CONTEXT_USAGE_TEXT_CLASS[contextUsageTone(contextUsage?.usagePercentage ?? null)]
|
||||
const pillLabel = [
|
||||
modelName || 'Select model',
|
||||
variant ? `${variant} variant` : null,
|
||||
contextUsage ? contextUsageAccessibleLabel(contextUsage) : null,
|
||||
].filter((part): part is string => part !== null).join(', ')
|
||||
|
||||
return (
|
||||
<ModelQuickSelect
|
||||
directory={directory}
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
session={modelSelectionSession}
|
||||
contextUsage={contextUsage}
|
||||
isCompacting={isCompacting}
|
||||
onCompact={onCompact}
|
||||
>
|
||||
<button
|
||||
aria-label={pillLabel}
|
||||
className="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-[220px] dark:border-border/30 flex flex-col items-start justify-center overflow-hidden"
|
||||
>
|
||||
<span className="truncate w-full text-left">{modelName || 'Select model'}</span>
|
||||
{(variant || pillUsage) && (
|
||||
<span className="flex w-full min-w-0 items-center gap-1 text-[10px] leading-tight">
|
||||
{variant && (
|
||||
<span className="truncate text-left capitalize text-highlight">{variant}</span>
|
||||
)}
|
||||
{variant && pillUsage && (
|
||||
<span aria-hidden="true" className="text-muted-foreground">·</span>
|
||||
)}
|
||||
{pillUsage && (
|
||||
<span
|
||||
data-testid="model-pill-context-usage"
|
||||
className={cn('shrink-0 font-medium', pillUsageTone)}
|
||||
>
|
||||
{pillUsage}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
{contextUsage && (
|
||||
<ContextUsageMeter
|
||||
percentage={contextUsage.usagePercentage}
|
||||
className="absolute inset-x-0 bottom-0"
|
||||
/>
|
||||
)}
|
||||
</button>
|
||||
</ModelQuickSelect>
|
||||
)
|
||||
})
|
||||
@@ -1,10 +1,12 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { useSyncExternalStore } from 'react'
|
||||
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { PromptInput } from './PromptInput'
|
||||
import { useComposerDraftStore } from '@/stores/composerDraftStore'
|
||||
import { useSessionStatus } from '@/stores/sessionStatusStore'
|
||||
import { createCommandActionsMock, createVoiceConversationControllerMock } from '@/test/test-utils'
|
||||
import type { ContextUsage } from '@/lib/contextUsage'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
useSTT: vi.fn(),
|
||||
@@ -22,6 +24,9 @@ const mocks = vi.hoisted(() => ({
|
||||
useHasBackgroundActivity: vi.fn(),
|
||||
useSendPrompt: vi.fn(),
|
||||
useContextUsage: vi.fn(),
|
||||
contextUsageListeners: new Set<() => void>(),
|
||||
contextUsageValue: null as ContextUsage | null,
|
||||
agentQuickSelectRenders: { current: 0 },
|
||||
sessionData: undefined as { revert?: { messageID: string } } | undefined,
|
||||
hasPendingPermission: vi.fn(() => false),
|
||||
setShowDialog: vi.fn(),
|
||||
@@ -70,7 +75,10 @@ vi.mock('@/contexts/EventContext', () => ({
|
||||
}))
|
||||
|
||||
vi.mock('@/components/agent/AgentQuickSelect', () => ({
|
||||
AgentQuickSelect: () => <div data-testid="agent-quick-select" />,
|
||||
AgentQuickSelect: () => {
|
||||
mocks.agentQuickSelectRenders.current += 1
|
||||
return <div data-testid="agent-quick-select" />
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/components/model/ModelQuickSelect', () => ({
|
||||
@@ -91,6 +99,22 @@ vi.mock('@/hooks/useSessionGoals', () => ({
|
||||
}))
|
||||
vi.mock('@/hooks/useSessionPermissionMode', () => ({ useSessionPermissionMode: () => ({ data: undefined }) }))
|
||||
|
||||
function subscribeContextUsage(listener: () => void) {
|
||||
mocks.contextUsageListeners.add(listener)
|
||||
return () => {
|
||||
mocks.contextUsageListeners.delete(listener)
|
||||
}
|
||||
}
|
||||
|
||||
function getContextUsageSnapshot() {
|
||||
return mocks.contextUsageValue
|
||||
}
|
||||
|
||||
function setContextUsage(usage: ContextUsage | null) {
|
||||
mocks.contextUsageValue = usage
|
||||
mocks.contextUsageListeners.forEach((listener) => listener())
|
||||
}
|
||||
|
||||
const createTestQueryClient = () => new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
})
|
||||
@@ -157,7 +181,10 @@ describe('PromptInput composer layout', () => {
|
||||
mocks.useSessionAgentStore.mockImplementation((selector: (state: unknown) => unknown) => selector({ setAgent: vi.fn() }))
|
||||
mocks.useSendErrorStore.mockImplementation((selector: (state: unknown) => unknown) => selector({ errors: {} }))
|
||||
mocks.useHasBackgroundActivity.mockReturnValue(false)
|
||||
mocks.useContextUsage.mockReturnValue({ usage: null, isLoading: false })
|
||||
mocks.useContextUsage.mockReturnValue(null)
|
||||
mocks.contextUsageListeners.clear()
|
||||
mocks.contextUsageValue = null
|
||||
mocks.agentQuickSelectRenders.current = 0
|
||||
mocks.sessionData = undefined
|
||||
})
|
||||
|
||||
@@ -249,8 +276,8 @@ describe('PromptInput composer layout', () => {
|
||||
})
|
||||
|
||||
describe('model pill context usage', () => {
|
||||
const setUsage = (usage: { totalTokens: number, usagePercentage: number | null, contextLimit: number | null }) => {
|
||||
mocks.useContextUsage.mockReturnValue({ usage, isLoading: false })
|
||||
const setUsage = (usage: ContextUsage) => {
|
||||
mocks.useContextUsage.mockReturnValue(usage)
|
||||
}
|
||||
|
||||
it('shows the percentage with its tone and the meter on the model pill', () => {
|
||||
@@ -305,6 +332,24 @@ describe('PromptInput composer layout', () => {
|
||||
expect(screen.queryByTestId('context-usage-meter')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('re-renders only the pill when the context usage changes', () => {
|
||||
mocks.useContextUsage.mockImplementation(() => useSyncExternalStore(subscribeContextUsage, getContextUsageSnapshot))
|
||||
setContextUsage({ totalTokens: 84000, usagePercentage: 42, contextLimit: 200000 })
|
||||
renderComponent()
|
||||
|
||||
const siblingRenders = mocks.agentQuickSelectRenders.current
|
||||
expect(screen.getByTestId('model-pill-context-usage')).toHaveTextContent('42%')
|
||||
expect(siblingRenders).toBeGreaterThan(0)
|
||||
|
||||
act(() => {
|
||||
setContextUsage({ totalTokens: 100000, usagePercentage: 50, contextLimit: 200000 })
|
||||
})
|
||||
|
||||
expect(screen.getByTestId('model-pill-context-usage')).toHaveTextContent('50%')
|
||||
expect(screen.getByTestId('model-pill-context-usage')).toHaveClass('text-warning')
|
||||
expect(mocks.agentQuickSelectRenders.current).toBe(siblingRenders)
|
||||
})
|
||||
|
||||
it('measures usage up to the staged revert boundary', () => {
|
||||
mocks.sessionData = { revert: { messageID: 'msg_2' } }
|
||||
renderComponent()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef, memo, useCallback, type KeyboardEvent } from 'react'
|
||||
import { useContextUsage } from '@/hooks/useContextUsage'
|
||||
import { useSendPrompt, useInterruptSession, useSendShell, useAgents, useSkills, useSession } from '@/hooks/useOpenCode'
|
||||
import { useSendPrompt, useInterruptSession, useSendShell, useAgents, useSkills } from '@/hooks/useOpenCode'
|
||||
import { useCommands } from '@/hooks/useCommands'
|
||||
import { useCommandHandler } from '@/hooks/useCommandHandler'
|
||||
import { useFileSearch } from '@/hooks/useFileSearch'
|
||||
@@ -28,12 +27,11 @@ import { IconToggleButton } from '@/components/ui/icon-toggle-button'
|
||||
|
||||
import { PromptSuggestions, type PromptSuggestion } from './PromptSuggestions'
|
||||
import { ComposerActivityBar } from './ComposerActivityBar'
|
||||
import { ModelQuickSelect } from '@/components/model/ModelQuickSelect'
|
||||
import { AgentQuickSelect } from '@/components/agent/AgentQuickSelect'
|
||||
import { VoiceConversation, type VoiceConversationHandle } from './VoiceConversation'
|
||||
import { PermissionModeToggle } from '@/components/session/PermissionModeToggle'
|
||||
import { SessionSandboxMenu } from '@/components/session/SessionSandboxMenu'
|
||||
import { ContextUsageMeter } from '@/components/session/ContextUsageMeter'
|
||||
import { ComposerModelPill } from './ComposerModelPill'
|
||||
import { ComposerToolsMenu } from './ComposerToolsMenu'
|
||||
import { useSessionGoal, useStartSessionGoal } from '@/hooks/useSessionGoals'
|
||||
import { useSessionPermissionMode } from '@/hooks/useSessionPermissionMode'
|
||||
@@ -41,8 +39,7 @@ import { detectMentionTrigger, parsePromptToInput, getFilename, getDirectory, ty
|
||||
import { matchText, rankByMatch, type MatchRange } from '@/lib/fuzzyMatch'
|
||||
import { getNextPrimaryAgentId } from '@/lib/primaryAgents'
|
||||
import { getNextFavoriteModel } from '@/lib/favoriteModels'
|
||||
import { randomId, cn } from '@/lib/utils'
|
||||
import { CONTEXT_USAGE_TEXT_CLASS, contextUsageAccessibleLabel, contextUsageTone, formatContextUsage } from '@/lib/contextUsage'
|
||||
import { randomId } from '@/lib/utils'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { findModelInfo, isSameModelSelection } from '@opencode-manager/shared/opencode'
|
||||
import { isOpenSessionGoal } from '@opencode-manager/shared/schemas'
|
||||
@@ -1015,18 +1012,9 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
|
||||
const { setShowDialog, hasForSession: hasPermissionsForSession } = usePermissions()
|
||||
const hasPendingPermissionForSession = hasPermissionsForSession(sessionID)
|
||||
const { hasVariants, currentVariant, cycleVariant } = useVariants(directory, modelSelectionSession)
|
||||
const { data: session } = useSession(sessionID, directory)
|
||||
const revertMessageID = session?.revert?.messageID
|
||||
const { usage: contextUsage } = useContextUsage(sessionID, directory, revertMessageID)
|
||||
const isCompacting = useSessionStatusForSession(sessionID).type === 'compact'
|
||||
const modelPillVariant = hasVariants && currentVariant ? currentVariant : null
|
||||
const modelPillUsage = contextUsage ? formatContextUsage(contextUsage) : null
|
||||
const modelPillUsageTone = CONTEXT_USAGE_TEXT_CLASS[contextUsageTone(contextUsage?.usagePercentage ?? null)]
|
||||
const modelPillLabel = [
|
||||
displayModelName || 'Select model',
|
||||
modelPillVariant ? `${modelPillVariant} variant` : null,
|
||||
contextUsage ? contextUsageAccessibleLabel(contextUsage) : null,
|
||||
].filter((part): part is string => part !== null).join(', ')
|
||||
const handleCompact = useCallback(() => commandActions.compact(''), [commandActions])
|
||||
|
||||
const handleAgentChange = useCallback((agentId: string) => {
|
||||
setLocalMode(agentId)
|
||||
@@ -1355,46 +1343,17 @@ return (
|
||||
</IconToggleButton>
|
||||
</>
|
||||
) : null}
|
||||
<ModelQuickSelect
|
||||
<ComposerModelPill
|
||||
directory={directory}
|
||||
sessionID={sessionID}
|
||||
modelName={displayModelName}
|
||||
variant={modelPillVariant}
|
||||
modelSelectionSession={modelSelectionSession}
|
||||
open={isModelPickerOpen}
|
||||
onOpenChange={setIsModelPickerOpen}
|
||||
session={modelSelectionSession}
|
||||
contextUsage={contextUsage}
|
||||
isCompacting={isCompacting}
|
||||
onCompact={() => commandActions.compact('')}
|
||||
>
|
||||
<button
|
||||
aria-label={modelPillLabel}
|
||||
className="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-[220px] dark:border-border/30 flex flex-col items-start justify-center overflow-hidden"
|
||||
>
|
||||
<span className="truncate w-full text-left">{displayModelName || 'Select model'}</span>
|
||||
{(modelPillVariant || modelPillUsage) && (
|
||||
<span className="flex w-full min-w-0 items-center gap-1 text-[10px] leading-tight">
|
||||
{modelPillVariant && (
|
||||
<span className="truncate text-left capitalize text-highlight">{modelPillVariant}</span>
|
||||
)}
|
||||
{modelPillVariant && modelPillUsage && (
|
||||
<span aria-hidden="true" className="text-muted-foreground">·</span>
|
||||
)}
|
||||
{modelPillUsage && (
|
||||
<span
|
||||
data-testid="model-pill-context-usage"
|
||||
className={cn('shrink-0 font-medium', modelPillUsageTone)}
|
||||
>
|
||||
{modelPillUsage}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
{contextUsage && (
|
||||
<ContextUsageMeter
|
||||
percentage={contextUsage.usagePercentage}
|
||||
className="absolute inset-x-0 bottom-0"
|
||||
/>
|
||||
)}
|
||||
</button>
|
||||
</ModelQuickSelect>
|
||||
onCompact={handleCompact}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user