mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-03 06:47:03 +00:00
feat(web): show unavailable Agents on demand (#1798)
* feat(web): add unavailable Agent visibility toggle * feat(web): show unavailable Agents on demand * chore: retrigger PR checks * fix(web): keep unavailable Agent disclosure order
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { render } from '@testing-library/react'
|
||||
import { fireEvent, render, screen } from '@testing-library/react'
|
||||
import { CREATABLE_AGENT_FLAVORS } from '@hapi/protocol'
|
||||
import type { AgentAvailabilityEntry } from '@hapi/protocol'
|
||||
|
||||
vi.mock('@/lib/use-translation', () => ({
|
||||
useTranslation: () => ({ t: (key: string) => key }),
|
||||
@@ -9,7 +10,11 @@ vi.mock('@/lib/use-translation', () => ({
|
||||
import { AgentSelector } from './AgentSelector'
|
||||
import type { AgentType } from './types'
|
||||
|
||||
function renderedAgentValues(agents: readonly AgentType[] = CREATABLE_AGENT_FLAVORS): string[] {
|
||||
function availableEntries(): AgentAvailabilityEntry[] {
|
||||
return CREATABLE_AGENT_FLAVORS.map((agent) => ({ agent, available: true }))
|
||||
}
|
||||
|
||||
function renderedAgentValues(agents: readonly AgentAvailabilityEntry[] = availableEntries()): string[] {
|
||||
const { container } = render(
|
||||
<AgentSelector
|
||||
agent={'claude' as AgentType}
|
||||
@@ -31,7 +36,58 @@ describe('AgentSelector', () => {
|
||||
expect(renderedAgentValues()).toEqual([...CREATABLE_AGENT_FLAVORS])
|
||||
})
|
||||
|
||||
it('renders only Agents reported available by the machine', () => {
|
||||
expect(renderedAgentValues(['claude', 'codex'])).toEqual(['claude', 'codex'])
|
||||
it('reveals unavailable Agents on demand with their reason', () => {
|
||||
render(
|
||||
<AgentSelector
|
||||
agent={'claude' as AgentType}
|
||||
agents={[
|
||||
{ agent: 'claude', available: true },
|
||||
{ agent: 'codex', available: false, reason: 'invalid_configuration' },
|
||||
]}
|
||||
isDisabled={false}
|
||||
onAgentChange={() => {}}
|
||||
/>
|
||||
)
|
||||
|
||||
expect(screen.queryByDisplayValue('codex')).not.toBeInTheDocument()
|
||||
const disclosure = screen.getByRole('button', { name: 'newSession.moreAgents' })
|
||||
expect(disclosure).toHaveAttribute('aria-expanded', 'false')
|
||||
|
||||
fireEvent.click(disclosure)
|
||||
|
||||
expect(screen.getByDisplayValue('codex')).toBeDisabled()
|
||||
expect(screen.getByTitle('newSession.agentUnavailableReason.invalidConfiguration')).toBeInTheDocument()
|
||||
expect(screen.getByRole('button', { name: 'newSession.hideOtherAgents' })).toHaveAttribute('aria-expanded', 'true')
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'newSession.hideOtherAgents' }))
|
||||
expect(screen.queryByDisplayValue('codex')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders only the entries supplied by the machine availability state', () => {
|
||||
expect(renderedAgentValues(availableEntries().filter(({ agent }) => agent === 'claude' || agent === 'codex')))
|
||||
.toEqual(['claude', 'codex'])
|
||||
})
|
||||
|
||||
it('keeps the disclosure between available and unavailable Agents', () => {
|
||||
render(
|
||||
<AgentSelector
|
||||
agent={'claude' as AgentType}
|
||||
agents={[
|
||||
{ agent: 'agy', available: false, reason: 'not_found' },
|
||||
{ agent: 'claude', available: true },
|
||||
]}
|
||||
isDisabled={false}
|
||||
onAgentChange={() => {}}
|
||||
/>
|
||||
)
|
||||
|
||||
const claude = screen.getByDisplayValue('claude')
|
||||
const disclosure = screen.getByRole('button', { name: 'newSession.moreAgents' })
|
||||
expect(claude.compareDocumentPosition(disclosure) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy()
|
||||
|
||||
fireEvent.click(disclosure)
|
||||
|
||||
const agy = screen.getByDisplayValue('agy')
|
||||
expect(disclosure.compareDocumentPosition(agy) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,15 +1,71 @@
|
||||
import { useState } from 'react'
|
||||
import { getFlavorLabel } from '@hapi/protocol'
|
||||
import type { AgentAvailabilityEntry } from '@hapi/protocol'
|
||||
import type { AgentType } from './types'
|
||||
import { AgentFlavorIcon } from '@/components/AgentFlavorIcon'
|
||||
import { useTranslation } from '@/lib/use-translation'
|
||||
|
||||
function ChevronIcon(props: { expanded: boolean }) {
|
||||
return (
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
className={`h-3.5 w-3.5 transition-transform ${props.expanded ? 'rotate-180' : ''}`}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="m6 9 6 6 6-6" />
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export function AgentSelector(props: {
|
||||
agent: AgentType
|
||||
agents: readonly AgentType[]
|
||||
agents: readonly Pick<AgentAvailabilityEntry, 'agent' | 'available' | 'reason'>[]
|
||||
isDisabled: boolean
|
||||
onAgentChange: (value: AgentType) => void
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const [showUnavailableAgents, setShowUnavailableAgents] = useState(false)
|
||||
const availableAgents = props.agents.filter((entry) => entry.available)
|
||||
const unavailableAgents = props.agents.filter((entry) => !entry.available)
|
||||
const visibleUnavailableAgents = showUnavailableAgents ? unavailableAgents : []
|
||||
|
||||
const renderAgent = (entry: Pick<AgentAvailabilityEntry, 'agent' | 'available' | 'reason'>) => {
|
||||
const unavailableReason = entry.available
|
||||
? null
|
||||
: entry.reason === 'invalid_configuration'
|
||||
? t('newSession.agentUnavailableReason.invalidConfiguration')
|
||||
: t('newSession.agentUnavailableReason.notFound')
|
||||
const label = getFlavorLabel(entry.agent)
|
||||
|
||||
return (
|
||||
<label
|
||||
key={entry.agent}
|
||||
className={`flex items-center gap-1.5 ${entry.available ? 'cursor-pointer' : 'cursor-not-allowed opacity-70'}`}
|
||||
title={unavailableReason ?? undefined}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="agent"
|
||||
value={entry.agent}
|
||||
checked={props.agent === entry.agent}
|
||||
onChange={() => props.onAgentChange(entry.agent as AgentType)}
|
||||
disabled={props.isDisabled || !entry.available}
|
||||
aria-label={unavailableReason ? `${label} (${unavailableReason})` : label}
|
||||
className="accent-[var(--app-link)]"
|
||||
/>
|
||||
<AgentFlavorIcon flavor={entry.agent} className="h-4 w-4 shrink-0" />
|
||||
<span className="text-sm">{label}</span>
|
||||
{unavailableReason ? (
|
||||
<span className="text-xs text-[var(--app-hint)]">({unavailableReason})</span>
|
||||
) : null}
|
||||
</label>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-1.5 px-3 py-3">
|
||||
@@ -17,24 +73,21 @@ export function AgentSelector(props: {
|
||||
{t('newSession.agent')}
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-x-3 gap-y-2">
|
||||
{props.agents.map((agentType) => (
|
||||
<label
|
||||
key={agentType}
|
||||
className="flex items-center gap-1.5 cursor-pointer"
|
||||
{availableAgents.map(renderAgent)}
|
||||
{unavailableAgents.length > 0 ? (
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={showUnavailableAgents}
|
||||
onClick={() => setShowUnavailableAgents((expanded) => !expanded)}
|
||||
className="inline-flex shrink-0 items-center gap-1 rounded-md border border-[var(--app-border)] px-2 py-1 text-xs text-[var(--app-link)] transition-colors hover:bg-[var(--app-subtle-bg)]"
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
name="agent"
|
||||
value={agentType}
|
||||
checked={props.agent === agentType}
|
||||
onChange={() => props.onAgentChange(agentType)}
|
||||
disabled={props.isDisabled}
|
||||
className="accent-[var(--app-link)]"
|
||||
/>
|
||||
<AgentFlavorIcon flavor={agentType} className="h-4 w-4 shrink-0" />
|
||||
<span className="text-sm">{getFlavorLabel(agentType)}</span>
|
||||
</label>
|
||||
))}
|
||||
<ChevronIcon expanded={showUnavailableAgents} />
|
||||
{showUnavailableAgents
|
||||
? t('newSession.hideOtherAgents')
|
||||
: t('newSession.moreAgents')}
|
||||
</button>
|
||||
) : null}
|
||||
{visibleUnavailableAgents.map(renderAgent)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||
import type { ApiClient } from '@/api/client'
|
||||
import type { ClaudeModelSummary, Machine, PiModelSummary } from '@/types/api'
|
||||
import type { AgentAvailabilityEntry, ClaudeModelSummary, Machine, PiModelSummary } from '@/types/api'
|
||||
import { saveNewSessionFormDraft } from './newSessionFormDraft'
|
||||
import {
|
||||
loadPreferredLaunchSettings,
|
||||
@@ -17,7 +17,7 @@ const mocks = vi.hoisted(() => ({
|
||||
checkPathsExists: vi.fn(),
|
||||
availableAgents: [
|
||||
'agy', 'claude', 'codex', 'dsh', 'copilot', 'cursor', 'grok', 'kimi', 'opencode', 'pi'
|
||||
].map((agent) => ({ agent, available: true })),
|
||||
].map((agent) => ({ agent, available: true })) as AgentAvailabilityEntry[],
|
||||
codexModelsLoading: false,
|
||||
agyModelsLoading: false,
|
||||
agyModels: [{ modelId: 'gemini-3.6-flash-low', name: 'Gemini 3.6 Flash (Low)' }],
|
||||
@@ -334,7 +334,7 @@ describe('NewSession launch preferences', () => {
|
||||
0,
|
||||
mocks.availableAgents.length,
|
||||
...['agy', 'claude', 'codex', 'dsh', 'copilot', 'cursor', 'grok', 'kimi', 'opencode', 'pi']
|
||||
.map((agent) => ({ agent, available: true }))
|
||||
.map((agent) => ({ agent, available: true })) as AgentAvailabilityEntry[]
|
||||
)
|
||||
mocks.codexModelsLoading = false
|
||||
mocks.agyModelsLoading = false
|
||||
@@ -386,6 +386,34 @@ describe('NewSession launch preferences', () => {
|
||||
expect(screen.queryByDisplayValue('claude')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('reveals unavailable Agents on demand and keeps them disabled', async () => {
|
||||
mocks.availableAgents.splice(
|
||||
0,
|
||||
mocks.availableAgents.length,
|
||||
{ agent: 'claude', available: true },
|
||||
{ agent: 'codex', available: true },
|
||||
{ agent: 'agy', available: false, reason: 'not_found' }
|
||||
)
|
||||
|
||||
render(
|
||||
<NewSession
|
||||
api={api}
|
||||
machines={[machine]}
|
||||
initialMachineId="machine-1"
|
||||
initialDirectory="C:\\repo"
|
||||
onSuccess={mocks.onSuccess}
|
||||
onCancel={() => {}}
|
||||
/>
|
||||
)
|
||||
|
||||
expect(screen.queryByDisplayValue('agy')).not.toBeInTheDocument()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'newSession.moreAgents' }))
|
||||
await waitFor(() => expect(screen.getByDisplayValue('agy')).toBeDisabled())
|
||||
expect(screen.getByDisplayValue('agy')).toHaveAccessibleName(/newSession\.agentUnavailableReason\.notFound/)
|
||||
expect(screen.getByDisplayValue('claude')).toBeEnabled()
|
||||
expect(screen.getByTestId('create')).toBeEnabled()
|
||||
})
|
||||
|
||||
it('does not claim Agents are missing when no machine is connected', () => {
|
||||
render(
|
||||
<NewSession
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState, type KeyboardEvent as ReactKeyboardEvent } from 'react'
|
||||
import type { ApiClient } from '@/api/client'
|
||||
import type { CodexDuplicateSessionGroup, CodexLocalSessionSummary, Machine, OpencodeLocalSessionSummary, PiLocalSessionSummary } from '@/types/api'
|
||||
import type { CodexCollaborationMode, GrokPermissionMode, PermissionMode, CopilotAgentMode } from '@hapi/protocol'
|
||||
import { getAgyModelChoices, splitAgyModelSelection, getClaudeEffortLevelsForModel, resolveClaudeEffortForModel } from '@hapi/protocol'
|
||||
import { CREATABLE_AGENT_FLAVORS, getAgyModelChoices, splitAgyModelSelection, getClaudeEffortLevelsForModel, resolveClaudeEffortForModel } from '@hapi/protocol'
|
||||
import type { AgentAvailabilityEntry, CodexCollaborationMode, GrokPermissionMode, PermissionMode, CopilotAgentMode } from '@hapi/protocol'
|
||||
import { codexModelAdvertisesFastTier } from '@/components/AssistantChat/codexFastMode'
|
||||
import { usePlatform } from '@/hooks/usePlatform'
|
||||
import { useMachinePathsExists } from '@/hooks/useMachinePathsExists'
|
||||
@@ -314,11 +314,24 @@ export function NewSession(props: {
|
||||
api: props.api,
|
||||
machineId,
|
||||
})
|
||||
const agentOptions = useMemo<AgentAvailabilityEntry[]>(() => {
|
||||
if (!machineId || agentAvailability.isLoading || agentAvailability.error) return []
|
||||
|
||||
const availabilityByAgent = new Map(
|
||||
agentAvailability.agents.map((entry) => [entry.agent, entry])
|
||||
)
|
||||
return CREATABLE_AGENT_FLAVORS
|
||||
.map((agentFlavor) => availabilityByAgent.get(agentFlavor) ?? {
|
||||
agent: agentFlavor,
|
||||
available: false,
|
||||
reason: 'not_found' as const,
|
||||
})
|
||||
}, [agentAvailability.agents, agentAvailability.error, agentAvailability.isLoading, machineId])
|
||||
const availableAgents = useMemo(
|
||||
() => agentAvailability.agents
|
||||
.filter((entry) => entry.available && entry.agent !== 'gemini')
|
||||
() => agentOptions
|
||||
.filter((entry) => entry.available)
|
||||
.map((entry) => entry.agent as AgentType),
|
||||
[agentAvailability.agents]
|
||||
[agentOptions]
|
||||
)
|
||||
const selectedAgentAvailable = availableAgents.includes(agent)
|
||||
|
||||
@@ -2052,7 +2065,7 @@ export function NewSession(props: {
|
||||
/>
|
||||
<AgentSelector
|
||||
agent={agent}
|
||||
agents={availableAgents}
|
||||
agents={agentOptions}
|
||||
isDisabled={isFormDisabled || agentAvailability.isLoading || Boolean(agentAvailability.error)}
|
||||
onAgentChange={handleAgentChange}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user