diff --git a/web/src/components/NewSession/AgentSelector.test.tsx b/web/src/components/NewSession/AgentSelector.test.tsx index 6f6e0ae7..8655d37b 100644 --- a/web/src/components/NewSession/AgentSelector.test.tsx +++ b/web/src/components/NewSession/AgentSelector.test.tsx @@ -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( { 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( + {}} + /> + ) + + 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( + {}} + /> + ) + + 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() }) }) diff --git a/web/src/components/NewSession/AgentSelector.tsx b/web/src/components/NewSession/AgentSelector.tsx index 24886e17..b0e2cdf5 100644 --- a/web/src/components/NewSession/AgentSelector.tsx +++ b/web/src/components/NewSession/AgentSelector.tsx @@ -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 ( + + ) +} + export function AgentSelector(props: { agent: AgentType - agents: readonly AgentType[] + agents: readonly Pick[] 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) => { + const unavailableReason = entry.available + ? null + : entry.reason === 'invalid_configuration' + ? t('newSession.agentUnavailableReason.invalidConfiguration') + : t('newSession.agentUnavailableReason.notFound') + const label = getFlavorLabel(entry.agent) + + return ( + + ) + } return (
@@ -17,24 +73,21 @@ export function AgentSelector(props: { {t('newSession.agent')}
- {props.agents.map((agentType) => ( -
) diff --git a/web/src/components/NewSession/index.test.tsx b/web/src/components/NewSession/index.test.tsx index ccc22dfb..dad51988 100644 --- a/web/src/components/NewSession/index.test.tsx +++ b/web/src/components/NewSession/index.test.tsx @@ -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( + {}} + /> + ) + + 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( (() => { + 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: { /> diff --git a/web/src/lib/locales/en.ts b/web/src/lib/locales/en.ts index 185196f1..04da328e 100644 --- a/web/src/lib/locales/en.ts +++ b/web/src/lib/locales/en.ts @@ -403,6 +403,10 @@ export default { 'newSession.runnerUpgradeRequired': 'Upgrade and restart this machine\'s HAPI runner before creating sessions.', 'newSession.noAvailableAgents': 'No supported Agents are installed on this machine.', 'newSession.agentUnavailable': 'The selected Agent is no longer available on this machine. Refresh and choose another Agent.', + 'newSession.agentUnavailableReason.notFound': 'Not installed or not on PATH', + 'newSession.agentUnavailableReason.invalidConfiguration': 'Invalid runner configuration', + 'newSession.moreAgents': 'More Agents', + 'newSession.hideOtherAgents': 'Hide other Agents', 'newSession.directoryOutsideWorkspaceRoots': 'Directory must be inside one of this machine\'s workspace roots.', 'newSession.model': 'Model', 'newSession.effort': 'Effort', diff --git a/web/src/lib/locales/zh-CN.ts b/web/src/lib/locales/zh-CN.ts index caffa11c..de8d2b8b 100644 --- a/web/src/lib/locales/zh-CN.ts +++ b/web/src/lib/locales/zh-CN.ts @@ -407,6 +407,10 @@ export default { 'newSession.runnerUpgradeRequired': '创建会话前,请升级并重启此机器上的 HAPI runner。', 'newSession.noAvailableAgents': '此机器上没有安装受支持的 Agent。', 'newSession.agentUnavailable': '所选 Agent 在此机器上已不可用,请刷新后选择其他 Agent。', + 'newSession.agentUnavailableReason.notFound': '未安装或不在 PATH 中', + 'newSession.agentUnavailableReason.invalidConfiguration': 'Runner 配置无效', + 'newSession.moreAgents': '更多 Agent', + 'newSession.hideOtherAgents': '收起其他 Agent', 'newSession.directoryOutsideWorkspaceRoots': '目录必须位于此机器配置的任一 workspace root 内。', 'newSession.model': '模型', 'newSession.effort': '思考强度',